0

here is my code:

private static JSONObject doGetRequest(String url) throws JSONException {
    System.out.println("doGetRequest > url : " + url);
    JSONObject json = null;
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response;
    System.out.println("doGetRequest > httpGet " + httpGet);
    try {
        System.out.println("doGetRequest > before response ");
        response = httpClient.execute(httpGet);
        System.out.println("doGetRequest > response " + response);
        if(response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                String result = convertStreamToString(instream);
                instream.close();

                json = new JSONObject(result);
            }
        } else {
            json = oDeskRestClient.genError(response.getStatusLine().getStatusCode(), response.getStatusLine().toString());
        }
    } catch (ClientProtocolException e) {
        json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: ClientProtocolException");
    } catch (IOException e) {
        json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: IOException");
    } catch (JSONException e) {
        json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: JSONException");  
    } catch (Exception e) {
        json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: Exception " + e.toString());
    } finally {
        httpGet.abort();
    }
    System.out.println("doGetRequest > json: " + json);
    return json;
}

I'm getting exception : Exception android.os.NetworkOnMainThreadException","code":"503" on this line : HttpClient httpClient = new DefaultHttpClient();

Can anyone help me to resolve this error?

Thanks

Pawan Developers
  • 377
  • 4
  • 16
  • where do you call this method? You can't use this method in Main Thread. – Gabriele Mariotti Apr 05 '13 at 22:33
  • Please google `android.os.NetworkOnMainThreadException`. You should have found http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception before asking this question. – Steven Byle Apr 05 '13 at 23:13

1 Answers1

1

Look at the documentation. Basically, you need to launch an AsyncTask (for instance) and in it, invoke your method.

From the second link:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

Once created, a task is executed very simply:

 new DownloadFilesTask().execute(url1, url2, url3);

In your case, of course, you need to call doGetRequest instead of Downloader.downloadFile(urls[i])

DigCamara
  • 5,540
  • 4
  • 36
  • 47