Search code examples
androidhttpconnection

Error while connecting with webservice


i write this code in main.java

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        try{
             HttpClient httpclient = new DefaultHttpClient();
             HttpPost httppost = new HttpPost("http://way2tutorial.com/json/index.php");
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
             HttpResponse response = httpclient.execute(httppost);
             HttpEntity entity = response.getEntity();
             InputStream is = entity.getContent();
             }catch(Exception e){
                 Log.e("log_tag", "Error in http connection"+e.toString());
            }

and also include

<uses-permission android:name="android.permission.INTERNET"/>

in manifestfile

but there is a error when run android application error in http

connectionandroid.os.NetworkOnMainThreadException


Solution

  • Starting from Honeycomb (v 3.0) you are not allowed to perform Networking task on main thread. Try doing it on AsyncTask or if you are really doing some trivial task try changing your minsdkversion in manifest lower than 11.

    http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html