在我的项目中 JSONObject 和 JSONArray 数据在第 8 级和第 10 级工作,但相同的代码在第 16 级不工作 为什么这里发生这种情况是我的代码
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response;
try {
response = httpclient.execute(httpget);
Log.i("response", "response" + response);
HttpEntity entity = response.getEntity();
Log.i("entity", "entity" + entity);
if (entity != null) {
InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
JSONObject json = new JSONObject(result);
JSONObject jsonOnb = json.getJSONObject("query").getJSONObject("pages") ;
JSONObject pagesObj = jsonOnb.getJSONObject(jsonOnb.names().getString(0));
Log.i("pageid", "pageid= " +pagesObj.get("pageid"));
Log.i("title", "title= " +pagesObj.get("title"));
instream.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
此代码在 8 级和 10 级工作,但不能在 16 级工作,在 16 级面临此错误
02-18 16:00:22.480: D/AndroidRuntime(605): Shutting down VM
02-18 16:00:22.480: W/dalvikvm(605): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-18 16:00:22.500: E/AndroidRuntime(605): FATAL EXCEPTION: main
02-18 16:00:22.500: E/AndroidRuntime(605): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a/com.example.a.MainActivity}: android.os.NetworkOnMainThreadException
02-18 16:00:22.500: E/AndroidRuntime(605): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-18 16:00:22.500: E/AndroidRuntime(605): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-18 16:00:22.500: E/AndroidRuntime(605): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-18 16:00:22.500: E/AndroidRuntime(605): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-18 16:00:22.500: E/AndroidRuntime(605): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 16:00:22.500: E/AndroidRuntime(605): at android.os.Looper.loop(Looper.java:137)
02-18 16:00:22.500: E/AndroidRuntime(605): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-18 16:00:22.500: E/AndroidRuntime(605): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:00:22.500: E/AndroidRuntime(605): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 16:00:22.500: E/AndroidRuntime(605): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-18 16:00:22.500: E/AndroidRuntime(605): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-18 16:00:22.500: E/AndroidRuntime(605): at dalvik.system.NativeStart.main(Native Method)
02-18 16:00:22.500: E/AndroidRuntime(605): Caused by: android.os.NetworkOnMainThreadException
02-18 16:00:22.500: E/AndroidRuntime(605): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
02-18 16:00:22.500: E/AndroidRuntime(605): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-18 16:00:22.500: E/AndroidRuntime(605): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-18 16:00:22.500: E/AndroidRuntime(605): at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-18 16:00:22.500: E/AndroidRuntime(605): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
02-18 16:00:22.500: E/AndroidRuntime(605): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-18 16:00:22.500: E/AndroidRuntime(605): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-18 16:00:22.500: E/AndroidRuntime(605): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-18 16:00:22.500: E/AndroidRuntime(605): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-18 16:00:22.500: E/AndroidRuntime(605): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-18 16:00:22.500: E/AndroidRuntime(605): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-18 16:00:22.500: E/AndroidRuntime(605): at com.example.a.MainActivity.onCreate(MainActivity.java:41)
02-18 16:00:22.500: E/AndroidRuntime(605): at android.app.Activity.performCreate(Activity.java:5008)
02-18 16:00:22.500: E/AndroidRuntime(605): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-18 16:00:22.500: E/AndroidRuntime(605): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-18 16:00:22.500: E/AndroidRuntime(605): ... 11 more
line 41 is
响应 = httpclient.execute(httpget);`