(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 ....