我从这里使用了一个progress spinner library。当我使用params AsyncTask<String, Integer, Double>
使用实例时它工作正常它完美无缺,并在UI上显示Progress微调器。
我刚修改它,因为我需要JSONArray作为输出,所以我将其修改为AsyncTask<String, Integer, JSONArray>
停止显示Progres微调器。所以我尝试了虚拟publishProgress((Integer) ((i / count) * 100));
它调用方法onProgressUpdate
,但进度微调器未显示。
AsyncTask<String, Integer, JSONArray> asynTask = new GetEmployeeDetails(MainActivity.this).execute("");
try {
response = asynTask.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Log.i("Resultz >>>>>>",response.toString());
}
并实施为
public class GetEmployeeDetails extends AsyncTask<String, Integer, JSONArray>
implements OnCancelListener {
ProgressHUD mProgressHUD;
private int statusCode = 0;
private Activity activity;
public GetEmployeeDetails(MainActivity activity) {
super();
this.activity = activity;
}
@Override
protected void onPreExecute() {
mProgressHUD = ProgressHUD.show(activity, "Posting Data...", true, false,
this);
super.onPreExecute();
}
@Override
protected JSONArray doInBackground(String... params) {
JSONArray resultJSONArray = null;
try {
resultJSONArray = getData(params[0]);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resultJSONArray;
}
protected void onPostExecute(JSONArray result) {
// pb.setVisibility(View.GONE);
mProgressHUD.dismiss();
super.onPostExecute(result);
showHTTPResponseMessage(statusCode);
}
protected void onProgressUpdate(Integer... progress) {
// pb.setProgress(progress[0]);
mProgressHUD.setMessage("Wait");
super.onProgressUpdate(progress);
}
public JSONArray getData(String valueIWantToSend) throws JSONException,
ClientProtocolException, IOException, URISyntaxException {
URI url = new URI("http://10.10.9.101:9393/users");
String result = "";
int count = 20;
long totalSize = 0;
for (int i = 0; i < count; i++) {
publishProgress((Integer) ((i / count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
InputStream inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if (inputStream != null) {
result = convertInputStreamToString(inputStream);
Log.i("Result >>>>> ", result);
} else
result = "Did not work!";
StatusLine statusLine = httpResponse.getStatusLine();
statusCode = statusLine.getStatusCode();
Log.e("TAG", "HTTP Status Code: " + statusLine.getStatusCode());
JSONArray jsonObject = new JSONArray(result.toString());
return jsonObject;
}
@Override
public void onCancel(DialogInterface arg0) {
this.cancel(true);
mProgressHUD.dismiss();
}
public void showHTTPResponseMessage(int messageCode) {
if (messageCode == 200) {
Toast.makeText(activity, "200 OOK Post Successfull", Toast.LENGTH_LONG)
.show();
}
if (messageCode == 404) {
Toast.makeText(activity, "404 Not Found. Check your posting server url",
Toast.LENGTH_LONG).show();
}
if (messageCode == 500) {
Toast.makeText(activity,
"500 Internal Server Error. Sorry We will back Soon!!",
Toast.LENGTH_LONG).show();
} else if (messageCode == 0)
Toast.makeText(activity, "WOOOOOOFFFF... Unknonwn error",
Toast.LENGTH_LONG).show();
}
private static String convertInputStreamToString(InputStream inputStream)
throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
}
除了未显示的微调器之外,每件工作都很好,为什么?
答案 0 :(得分:0)
您不应该使用response = asynTask.get();
。因为它仍在等待响应,所以窗口无法附加。
您可以在onPostExecute()中调用另一个方法,而不是使用get(),该方法负责根据响应执行操作。