Search code examples
androidweb-servicesresponseandroid-api-levels

Android project working in 2.2(Level8) and 2.3.3(level10) not working in 4.2.2(level16)?


in my project JSONObject and JSONArray data work in level 8 and level 10 but same code not working level 16 why this happen here is my code

  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();
    }
}

this code working in level 8 and 10 but not working level 16 facing this errors in level 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 isresponse = httpclient.execute(httpget);`


Solution

  • You need to add these two line of code on your onCreate() function

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    
    StrictMode.setThreadPolicy(policy);