My question: AndroidManifest.xml add the following two lines, the program runs to hang (if you remove these two lines, normal operation), print information:
android: targetSdkVersion = "17" />
This code is a simple example of using http httpclient written.
Add permissions
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
getServerData ("http://www.baidu.com");
}
private void getServerData (String url) {
HttpClient client = new DefaultHttpClient ();
HttpGet request;
/ / HttpPost request;
try {
request = new HttpGet (new URI (url));
/ / Request = new HttpPost (new URI (url));
HttpResponse response = client.execute (request);
/ / Determine whether the request is successful
if (response.getStatusLine (). getStatusCode () == 200) {
HttpEntity entity = response.getEntity ();
if (entity! = null) {
String out = EntityUtils.toString (entity);
new AlertDialog.Builder (this) setMessage (out) create () show ();...
}
}
else {
HttpEntity entity = response.getEntity ();
if (entity! = null) {
. String out = "errcode:" + response.getStatusLine () getStatusCode () + "\ t \ n" + EntityUtils.toString (entity);
new AlertDialog.Builder (this) setMessage (out) create () show ();...
}
}
}
catch (URISyntaxException e) {
e.printStackTrace ();
}
catch (ClientProtocolException e) {
e.printStackTrace ();
}
catch (IOException e) {
e.printStackTrace ();
}
}
Error LOG:
10-29 22:43:18.103: W / System.err (2004): at dalvik.system.NativeStart.main (Native Method)
10-29 22:43:18.423: D / gralloc_goldfish (2004):. Emulator without GPU emulation detected
10-29 22:45:06.933: D / AndroidRuntime (2094): Shutting down VM
10-29 22:45:06.973: W / dalvikvm (2094): threadid = 1: thread exiting with uncaught exception (group = 0x414c4700)
10-29 22:45:07.013: E / AndroidRuntime (2094): FATAL EXCEPTION: main
10-29 22:45:07.013: E / AndroidRuntime (2094): java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.httpdemo / com.example.httpdemo.MainActivity}: android.os.NetworkOnMainThreadException
10-29 22:45:07.013: E / AndroidRuntime (2094): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2211)
10-29 22:45:07.013: E / AndroidRuntime (2094): at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2261)
10-29 22:45:07.013: E / AndroidRuntime (2094): at android.app.ActivityThread.access $ 600 (ActivityThread.java: 141)
10-29 22:45:07.013: E / AndroidRuntime (2094): at android.app.ActivityThread $ H.handleMessage (ActivityThread.java: 1256)
10-29 22:45:07.013: E / AndroidRuntime (2094): at android.os.Handler.dispatchMessage (Handler.java: 99)
10-29 22:45:07.013: E / AndroidRuntime (2094): at android.os.Looper.loop (Looper.java: 137)
10-29 22:45:07.013: E / AndroidRuntime (2094): at android.app.ActivityThread.main (ActivityThread.java: 5103)
10-29 22:45:07.013: E / AndroidRuntime (2094): at java.lang.reflect.Method.invokeNative (Native Method)
10-29 22:45:07.013: E / AndroidRuntime (2094): at java.lang.reflect.Method.invoke (Method.java: 525)
10-29 22:45:07.013: E / AndroidRuntime (2094): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 737)
10-29 22:45:07.013: E / AndroidRuntime (2094): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 553)
10-29 22:45:07.013: E / AndroidRuntime (2094): at dalvik.system.NativeStart.main (Native Method)
10-29 22:45:07.013: E / AndroidRuntime (2094): Caused by: android.os.NetworkOnMainThreadException
10-29 22:45:07.013: E / AndroidRuntime (2094): at android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork (StrictMode.java: 1133)
10-29 22:45:07.013: E / AndroidRuntime (2094): at java.net.InetAddress.lookupHostByName (InetAddress.java: 385)
10-29 22:45:07.013: E / AndroidRuntime (2094): at<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
UI main thread can not do network operations. You have to start a thread, the network operations are put here.
Reply:
The targetSdkVersion also not being given a little piecemeal and this is upstairs threading issues, the old version of the api is no requirement does not the UI thread and sub-thread error. The new api demanding, relatively speaking, more reasonable!
Reply:
Threading issues, Android 4.0 or above is not connected to the main thread http this time-consuming operation (of course, 2.3 is still possible, but not recommended), should be placed in the child thread, and then when the child thread to complete broadcast recurrence good , message or, notify the main thread UI changes accordingly.
If you're in the Http connection, the main thread does not require additional operations, then it pops up a progressDialog idling, and then after the child thread to complete the operation, dimiss out progressDialog enough.

Reply:
Thank you for three enthusiastic answer, the real cause of the problem has been found. Can give a simple example of thread operations?
Results posted tomorrow afternoon.
Reply:
Use AsyncTask to do network operating just fine.
Reply:
No comments:
Post a Comment