在我的項目的JSONObject和JSONArray數據工作在8級和10級,但同樣的代碼無法正常工作16爲什麼發生在這裏的水平是我的代碼在2.2(Level8)和2.3.3(level10)中工作的Android項目在4.2.2(level16)中不起作用?
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)面臨着這樣的錯誤;`
你在主線程中調用網絡操作? – Renjith 2013-02-18 10:56:08
網絡操作意味着INTERNET權限.... – NagarjunaReddy 2013-02-18 10:58:06
共振爲負面標記...... – NagarjunaReddy 2013-02-18 10:58:55