2013-04-05 108 views
0

这里是我的代码:异常android.os.NetworkOnMainThreadException “ ”代码“: ”503“

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”, “代码”:“503 “ on this line:HttpClient httpClient = new DefaultHttpClient();

任何人都可以帮我解决这个错误吗?

谢谢

+0

你在哪儿叫这个方法?您不能在主线程中使用此方法。 – 2013-04-05 22:33:01

+0

请谷歌'android.os.NetworkOnMainThreadException'。在问这个问题之前,你应该找到http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception。 – 2013-04-05 23:13:26

+0

谢谢史蒂芬,它的工作。 – 2013-04-06 10:41:39

回答

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])

+0

谢谢,它帮助我解决了我的错误。 – 2016-08-22 13:25:49