在我的应用程序中,我有一个活动,负责通过以JSON格式从远程服务器获取数据来显示列表中的结果。但是当从服务器返回数据时,我的活动会冻结特定的响应。即status_code = -1,它存储在 jsonResult 变量中,但是当服务器返回其他状态代码时,它可以正常工作。
示例2来自服务器的响应消息
{"user_data":[],"status_message":"ERROR","status_code":-1}
{"user_data":[{"booked_slots":"1","slot_no":"2","place_name":"Centaurus","person_name":"faisal","slot_status":"Reserved","vehicle_no":"abc","startTime":"2","endTime":"3"}],"status_message":"OK","status_code":1}
每当返回status_code = -1时,活动会以圆形等待指针冻结,请等待,不再继续。
AsyncTask代码:
private class AsyncDataClass extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String responseBody = "";
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(params[0]);
String jsonResult = "";
try {
HttpResponse response = httpClient.execute(httpPost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
System.out.println("Returned Json object " + jsonResult.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonResult;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ShowSlots.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("Resulted Value: " + result);
if(result.equals("") || result == null){
Toast.makeText(ShowSlots.this, "Server connection failed", Toast.LENGTH_LONG).show();
return;
}
int jsonResult = returnParsedJsonObject(result);
if(jsonResult == -1){
Toast.makeText(ShowSlots.this, "ALL Slots Are Available", Toast.LENGTH_LONG).show();
return;
}
if(jsonResult == 1){
Toast.makeText(ShowSlots.this, "Got Parking Spots", Toast.LENGTH_LONG).show();
try {
JSONObject jsonObj = new JSONObject(result);
// Getting JSON Array node
parkings = jsonObj.getJSONArray("user_data");
// looping through All Contacts
for (int i = 0; i < parkings.length(); i++) {
JSONObject c = parkings.getJSONObject(i);
String slot = c.getString(TAG_SLOT);
serverReturn.add(slot);
}
rts= new ArrayList<slots>();
String status="";
int counter = 1;
for(int i=0;i<availableParkings;i++){
for(int j = 0; j<serverReturn.size();j++){
if(counter==Integer.parseInt(serverReturn.get(j))){
status="Reserved";
break;
}
else{
status="Available";
}
}
rts.add(new slots(parkingName, counter, status));
counter++;
}
refreshDisplay();
} catch (JSONException e) {
e.printStackTrace();
}
if (pDialog.isShowing())
pDialog.dismiss();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = br.readLine()) != null) {
answer.append(rLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return answer;
}
}
答案 0 :(得分:0)
当jsonResult
为-1时,您有一个return语句,导致onPostExecute()
返回而调用pDialog.dismiss()
:
if(jsonResult == -1){
Toast.makeText(ShowSlots.this, "ALL Slots Are Available", Toast.LENGTH_LONG).show();
return;
}
在不关闭对话框的情况下,它看起来好像应用程序仍在等待。您需要检查代码并确保在onPostExecute()
返回之前始终关闭对话框;