2013-04-07 90 views
0

我正在使用JSONParser.makeHttpRequest獲取並張貼一些數據。當連接速度慢時,需要一段時間才能得到響應,我想將超時限制設置爲5秒。我發現this post顯示瞭如何設置HttpResponse超時,但無法將其與JSONParser集成。如何在Android中爲JSONParser.makeHttpRequest設置連接超時?

這裏是我的代碼使用的HttpRequest

JSONParser jParser = new JSONParser(); 
List<NameValuePair> params = new ArrayList<NameValuePair>(); 
JSONObject json = jParser.makeHttpRequest(url, "GET", params); 

我會很感激,如果我能得到一個代碼片段或其他職位其他地方的鏈接。

回答

0

您可以使用Asynctask,並在一段時間後停止請求。 參見示例here

+0

爲什麼我沒有想到這一點,我已經使用Asy nctask,但不能想到這個非常簡單的方法。謝謝! – 2013-04-07 21:30:58

2

下面是代碼:

public class JSONParser { 

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

// constructor 
public JSONParser() { 
    timeout = new Values().gettimeout(); 
} 

// 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 { 
     HttpParams httpParameters = new BasicHttpParams(); 
     HttpConnectionParams.setSoTimeout(httpParameters, timeout); 
     // check for request method 
     if(method == "POST"){ 
      // request method is POST 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 
      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(httpParameters); 
      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(); 
    } 

    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; 

} 
} 

的祕訣就是:

 HttpParams httpParameters = new BasicHttpParams(); 
    HttpConnectionParams.setSoTimeout(httpParameters, timeout); 

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 

設置德的HttpParams適應你的代碼來做到這一點;)

+0

一切都好。但是當連接超時時會發生什麼。我沒有看到任何處理代碼(例外或任何...)。謝謝。 – wmac 2015-01-14 19:48:56

+0

這對我來說適用於較慢的連接,增加請求超時的值將完成工作。我的應用在wifi上運行良好,但是在移動數據上崩潰,但是當我增加超時時,它完美運行。信貸去@Tobias S. Lucian – Dipen 2015-01-21 16:21:36