Android4.0 calls the http interface php website api

Source: Internet
Author: User
(I just learned from cainiao) I read the online tutorial and wrote a simple HttpGet test. A bunch of errors are reported! Code: publicstaticStringgetApi (Stringurl) {Stringcontnull; HttpGethttpGetnewHttpGet (url); DefaultHttpClienthttpClientnewDefaultHttpClient ();

(I just learned from cainiao) I read the online tutorial and wrote a simple HttpGet test. A bunch of errors are reported! Code: public static String getApi (String url) {String cont = null; HttpGet httpGet = new HttpGet (url); DefaultHttpClient httpClient = new DefaultHttpClient ();

(I just learned from cainiao) I read the online tutorial and wrote a simple HttpGet test. A bunch of errors are reported!


Code:


 public static String getApi(String url){               String cont = null;               HttpGet httpGet = new HttpGet(url);                                    DefaultHttpClient httpClient = new DefaultHttpClient();                try {                   HttpResponse httpResponse = httpClient.execute(httpGet);                    int reCode = httpResponse.getStatusLine().getStatusCode();                   if (reCode == HttpStatus.SC_OK) {                       cont = EntityUtils.toString(httpResponse.getEntity());                       return cont;                   }               } catch (ClientProtocolException e) {                   e.printStackTrace();               } catch (IOException e) {                   e.printStackTrace();               }               return "";    }


Report thread problems, read online information and say that from 2.3 onwards, the thread must be run in the thread, and then write a variety of online statements. Then, the system reports an error... then... wipe or not .... write it yourself ..... hope to help new users

Initialize in onCreate

Handler = new Handler (); // Of course, the private Handler handler must be defined at the top;

Then write in your trigger event


public void login_submit(View v){        new Thread(){            @Override            public void run() {                String url = "http://192.168.1.188/123.html";                 rs = HttpApi.getApi(url);                                handler.post(new Runnable() {                    @Override                    public void run() {                        Toast.makeText(login.this,rs,Toast.LENGTH_SHORT).show();                    }                });            }}.start();    }


Okay. Finally

Add a permission to allow Internet access in AndroidManifest. xml.


OK ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.