0

I develop android application and every thing is right no error but when I run the code i found this error below and when I track the code it's stop on this line :

HttpResponse httpResponse = httpClient.execute(httpPost);

the part of the code :

try {
        // defaultHttpClient

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        Log.d("w", "2a json...................");
        HttpResponse httpResponse = httpClient.execute(httpPost);
        Log.d("w", "2b json...................");
        HttpEntity httpEntity = httpResponse.getEntity();
        Log.d("w", "2c json...................");
        is = httpEntity.getContent();
        Log.d("w", "3 json...................");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

the errors:

threadid=1: thread exiting with uncaught exception (group=0x410702a0)
FATAL EXCEPTION: main
android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
    at libcore.io.IoBridge.connect(IoBridge.java:112)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
    at java.net.Socket.connect(Socket.java:842)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – assylias Mar 15 '13 at 16:22
  • http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html – assylias Mar 15 '13 at 16:22

1 Answers1

0

Create an Asynctask and do your network call inside doInBackground method

here is your code

class ABCTask extends AsyncTask<String, Void, Void> {

    private Exception exception;

    protected void doInBackground(String... urls) {
       try {
        // defaultHttpClient

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        Log.d("w", "2a json...................");
        HttpResponse httpResponse = httpClient.execute(httpPost);
        Log.d("w", "2b json...................");
        HttpEntity httpEntity = httpResponse.getEntity();
        Log.d("w", "2c json...................");
        is = httpEntity.getContent();
        Log.d("w", "3 json...................");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }

    protected void onPostExecute() {

// write what u want to do after completion of the doInBackground method
    }
 }

 new ABCTask().execute();
Amitabh Sarkar
  • 1,281
  • 1
  • 13
  • 26