我正在使用Async任务类来解析'doInBackground'中的JSON文件,并将结果添加到'onPostExecute'中的列表视图中,但我使用的进度对话框永远不会停止,结果永远不会发布。我正在使用此代码:
public class JSONTask extends AsyncTask<String,String,JSONObject>
{
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AnswerActivity.this);
pDialog.setMessage("Getting Answers ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... params) {
try {
answersJson = makeRequest(requestUrl);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return answersJson;
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
try {
if(mJSONArr!=null){
mJSONArr = jsonObject.getJSONArray("items");
for(int i=0;i<mJSONArr.length();i++)
{
String val = null;
ob2 = mJSONArr.getJSONObject(i);
if(ob3!=null){ob3 = ob2.getJSONObject("owner");}
answers[i] = new Answer(ob2.getString("body"),ob3.getString("display_name"),ob2.getString("score"));
}
adapter = new AnswersAdapter(AnswerActivity.this,
R.layout.answer_list_item, answers);
answerList.setAdapter(adapter);
pDialog.dismiss();}
//url = "https://api.stackexchange.com/2.2/search?order=desc&sort=activity&";
} catch (JSONException e) {
e.printStackTrace();
}
}
}
这是makeRequest函数:
private JSONObject makeRequest(String url) throws IOException, JSONException {
JSONObject response;
String jsonString;
HttpClient httpclient = new DefaultHttpClient();
// create the request
HttpUriRequest request = new HttpGet(url);
request.addHeader("Accept-Encoding", "gzip");
// execute the request
HttpResponse resp = httpclient.execute(request);
StatusLine statusLine = resp.getStatusLine();
// check the request response status. Should be 200 OK
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
Header contentEncoding = resp.getFirstHeader("Content-Encoding");
InputStream instream = resp.getEntity().getContent();
// was the returned response gzip'ed?
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
StringBuilder responseString = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
responseString.append(line);
}
jsonString = responseString.toString();
response = new JSONObject(jsonString);
} else {
resp.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
return response;
}
如何修复此问题并在列表视图中查看结果?
谢谢!
答案 0 :(得分:1)
在开始onPostExecute();
pDialog.dismiss();
喜欢这个
@Override
protected void onPostExecute(JSONObject jsonObject) {
if( pDialog!=null && pDialog.isShowing())
pDialog.dismiss();
....
....
.... Rest of your code
}
答案 1 :(得分:1)
这可能会对你有所帮助。
将ComboBox_ColumnSchedule[i] = new ComboBox();
ComboBox_ColumnSchedule[i].Id = "Cbx_" + id;
ComboBox_ColumnSchedule[i].SelectedIndexChanged += ComboBox_SelectedIndexChanged;
...
private void ComboBox_SelectedIndexChanged(object sender,
System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
//replace with your criteria
ComboBox_BeamSchedule.Visible = comboBox.Id == "Cbx_2";
...
}
替换为
onPostExecute