-1

in my app i call a webservice..... and it shows some error. the error is given below..

 04-26 10:00:53.682: E/AndroidRuntime(1600): FATAL EXCEPTION: main
 04-26 10:00:53.682: E/AndroidRuntime(1600): android.os.NetworkOnMainThreadException
 04-26 10:00:53.682: E/AndroidRuntime(1600):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
 04-26 10:00:53.682: E/AndroidRuntime(1600):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
 04-26 10:00:53.682: E/AndroidRuntime(1600):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
 04-26 10:00:53.682: E/AndroidRuntime(1600):    at libcore.io.IoBridge.connect(IoBridge.java:112)
 04-26 10:00:53.682: E/AndroidRuntime(1600):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
 04-26 10:00:53.682: E/AndroidRuntime(1600):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
 04-26 10:00:53.682: E/AndroidRuntime(1600):    at java.net.Socket.connect(Socket.java:842)
 04-26 10:00:53.682: E/AndroidRuntime(1600):    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
 04-26 10:00:53.682: E/AndroidRuntime(1600):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)

i call the web service as given below....

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostReq = new HttpPost(serverURL+url);
ObjectMapper mapper = new ObjectMapper();
StringEntity se = null;

try {
    String jSonString = mapper.writeValueAsString(obj);
    se = new StringEntity(jSonString);
    se.setContentType("application/json");
    httpPostReq.setEntity(se);
    httpResponse = httpclient.execute(httpPostReq);
    HttpEntity resultEntity = httpResponse.getEntity();
    inputStream=resultEntity.getContent();
    resultString = convertStreamToString(inputStream);
    Log.e("result",resultString);
}

but this one is working on the emulator.... but not working with device.. i test this using a wi-fi connected tablet... but its not working..... pls help.....me......

Vladimir Mironov
  • 30,514
  • 3
  • 65
  • 62
Vikky
  • 933
  • 2
  • 15
  • 29

2 Answers2

0

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

You should use AyncTask.

private class MyTask extends AsyncTask<URL, Integer, String> {
 protected Long doInBackground(URL... urls) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httpPostReq = new HttpPost(urls[0]);
    ObjectMapper mapper = new ObjectMapper();
    StringEntity se = null;

    try {
        String jSonString = mapper.writeValueAsString(obj);
        se = new StringEntity(jSonString);
        se.setContentType("application/json");
        httpPostReq.setEntity(se);
        httpResponse = httpclient.execute(httpPostReq);
        HttpEntity resultEntity = httpResponse.getEntity();
        inputStream=resultEntity.getContent();
        resultString = convertStreamToString(inputStream);
        Log.e("result",resultString);
        }
         return resultString;
 }

 protected void onProgressUpdate(Integer... progress) {
     // progress
 }

 protected void onPostExecute(String result) {
     // result
 }
}

use like

 new MyTask().execute(serverURL+url);
Bhavesh Hirpara
  • 22,255
  • 15
  • 63
  • 104
0

Use this code It will help,

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
No_Rulz
  • 2,679
  • 1
  • 20
  • 33
  • 1
    It may work but that a very bad practice I wouldn't recommend. The exception was added to prevent what you are suggesting in the first place.
    AsyncTasks are here for it.
    – Lazy Ninja Apr 26 '13 at 05:48