2012-10-25 65 views
0

我想下面的代碼從服務器下載一個json文件。然而,我的應用程序行爲怪異。有時候,json會在1-2秒內下載,有時會永久卡住這個函數。我也嘗試了像HttpUrlConnection一樣的其他下載方式。但是,這也沒有幫助。任何人都可以建議我修復它!無響應的HttpClient

public String getJSONString(String url) { 
     String json = null; 
     HttpClient httpclient = null; 
     try { 
      Log.d("MARKER","PARSING SE PEHLE"); 
      HttpParams params = new BasicHttpParams(); 
      Log.d("MARKER","1st line"); 
      HttpClient httpclient = new DefaultHttpClient(params);    HttpPost httpPost = new HttpPost(url); 

      HttpResponse httpResponse = httpclient.execute(httpPost); 

      HttpEntity httpEntity = httpResponse.getEntity();  
      is = httpEntity.getContent(); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 80000); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) 
      {sb.append(line + "\n");} 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 
     Log.d("MARKER","DOWNLOAD COMPLETE"); 
     HttpClientProvider.safeClose(httpclient); 
     return json; 

    } 

回答

0

試試這個:

public class JSONParser { 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    // constructor 
    public JSONParser() { 

    } 

    // function get json from url 
    // by making HTTP POST or GET method 
    public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) { 

     // Making HTTP request 
     try { 

     // check for request method 
     if (method == "POST") { 
      // request method is POST 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     } else if (method == "GET") { 
      // request method is GET 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      String paramString = URLEncodedUtils.format(params, "utf-8"); 
      url += "?" + paramString; 
      HttpGet httpGet = new HttpGet(url); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 
     } 

     } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 

     if (is == null) 
     return new JSONObject(); 

     try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
     } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
     jObj = new JSONObject(json); 
     } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 
} 

您可以填寫相關對象的AsyncTask以避免另一個問題。玩得開心

String SERVER_URL =""; 


List<NameValuePair> params = new ArrayList<NameValuePair>(); 
// getting JSON string from URL 
JSONObject json = jsonParser.makeHttpRequest(SERVER_URL, "GET", params); 
+0

什麼是'params'參數? – gauravsapiens

+0

只看我新增加的帖子 –

+0

問題存在! – gauravsapiens