【问题标题】:Exception android.os.NetworkOnMainThreadException","code":"503"异常 android.os.NetworkOnMainThreadException","code":"503"
【发布时间】:2013-04-06 06:36:42
【问题描述】:

这是我的代码:

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

我遇到异常:异常 android.os.NetworkOnMainThreadException","code":"503" 在这一行: HttpClient httpClient = new DefaultHttpClient();

谁能帮我解决这个错误?

谢谢

【问题讨论】:

  • 你在哪里调用这个方法?您不能在主线程中使用此方法。
  • 请谷歌android.os.NetworkOnMainThreadException。你应该在问这个问题之前找到stackoverflow.com/questions/6343166/…
  • 感谢 Steven,它成功了。

标签: android


【解决方案1】:

查看documentation。基本上,您需要启动一个AsyncTask(例如)并在其中调用您的方法。

来自第二个链接:

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

一旦创建,任务就会非常简单地执行:

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

在你的情况下,当然,你需要调用doGetRequest 而不是Downloader.downloadFile(urls[i])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 2014-09-03
    • 2013-11-06
    • 2018-12-27
    • 2016-04-05
    • 1970-01-01
    相关资源
    最近更新 更多