这是我的 doInBackground() 方法,我在其中调用了其他类中的 makeHttpRequest() 方法。
protected String doInBackground(Integer... args) {
// Building Parameters
String parameter1 = "tenant";
String parameter2 = "price";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("person",parameter1));
params.add(new BasicNameValuePair("price",parameter2));
JSONObject json = jParser.makeHttpRequest(requiredurl, "POST", params);
Log.d("Details", json.toString());
int success = json.getInt("connected");
if (success == 1) {
//blah blah
}
}
makeHttpRequest() 方法:
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "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();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
................
....................... // Here the result is extracted and made it to json object
.............................
// return JSON
return jObj; // returning the json object to the method that calls.
}
因此,通过上面的代码,对服务器的实际调用是通过这一行进行的 -> HttpResponse httpResponse = httpClient.execute(httpPost);
当我的服务器启动时,一切正常,但是当它关闭时,进度条会不断加载。我想处理这种情况。做了很多搜索,但只能找到这篇文章。这看起来很适合我的情况,因为即使我的想法是等待 10 秒来获得响应,如果超过了超时时间,我需要处理它以在 catch 块中显示消息。但我无法根据我的代码实现它。有人可以帮我吗?我将非常感谢。