如何在Android中设置JSONParser.makeHttpRequest的连接超时?

如何在Android中设置JSONParser.makeHttpRequest的连接超时?,android,httprequest,android-internet,Android,Httprequest,Android Internet,我正在使用JSONParser.makeHttpRequest获取并发布一些数据。当连接缓慢时,需要一段时间才能得到响应,我想将超时限制设置为5秒。我发现了如何设置HttpResponse超时,但无法将其与JSONParser集成 下面是我用于HttpRequest的代码 JSONParser jParser = new JSONParser(); List<NameValuePair> params = new ArrayList<NameValuePair>(); J

我正在使用
JSONParser.makeHttpRequest
获取并发布一些数据。当连接缓慢时,需要一段时间才能得到响应,我想将超时限制设置为5秒。我发现了如何设置HttpResponse超时,但无法将其与JSONParser集成

下面是我用于HttpRequest的代码

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

如果我能从其他地方得到一个代码片段或到另一篇文章的链接,我将不胜感激

您可以使用Asynctask,并在一段时间后停止请求。 参见示例。

以下是代码:

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;

}
}
并在中设置de-HttpParams

 DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

调整代码以实现此目的;)

为什么我没有想到这一点,我已经使用了Asynctask,但不能想到那个非常简单的方法。谢谢都很好。但是当连接超时时会发生什么呢。我没有看到任何处理代码(异常或其他…)。谢谢。这对我来说很有效,因为连接速度较慢,增加请求超时值就可以了。我的应用程序在wifi上运行良好,但在移动数据上崩溃,但当我增加超时时,它工作得非常好。归功于@Tobias S.Lucian
 DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);