我会解释我的问题。
我有一个从上一个活动中检索数据的活动。当我点击我的按钮时,方法invokeSendQuestionChoose()被执行。
此方法执行两项操作:在两个不同的资源上发送http请求。
第一个资源是/ nextquestion。我想用新恢复的信息刷新我的Checkbox和TextView。
OnCreate中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question_layout);
idQuestion = (Integer) getIntent().getSerializableExtra("idQuestion");
question = (String) getIntent().getSerializableExtra("question");
answer = (String) getIntent().getSerializableExtra("answer");
btn_yes = (Button) findViewById(R.id.btn_yes);
tv_title_question.setText(question);
cb_title = (CheckBox) findViewById(R.id.cb_title);
...
}
单击按钮时的方法:
public void invokeSendQuestionChoose(View view){
invokeSendQuestionChoose();
invokeNextQuestion();
}
方法http请求:
private void invokeNextQuestion(){
class NextQuestionChooseAsync extends AsyncTask<String, Void, ArrayList<String>> {
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
// loadingDialog = ProgressDialog.show(QuestionActivity.this, "Please wait", "Loading...");
}
@Override
protected ArrayList<String> doInBackground(String... params) {
...
try {
//Http request to communicate with our system
HttpClient httpClient = new DefaultHttpClient();
List<BasicNameValuePair> parameters = new LinkedList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("idsession", String.valueOf(idSession)));
HttpGet httpGet = new HttpGet();
URI website = new URI("http://192.168.43.32:5001/nextquestion" + "?"+ URLEncodedUtils.format(parameters, "utf-8"));
httpGet.setURI(website);
String base64EncodedCredentials = Base64.encodeToString(
(email + ":" + password).getBytes(),
Base64.NO_WRAP);
Log.d("test",base64EncodedCredentials);
httpGet.setHeader("Authorization", "Basic " + base64EncodedCredentials);
HttpResponse response = httpClient.execute(httpGet);
statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
Log.d("result1", result);
//Manage JSON result
JSONObject jsonObject = new JSONObject(result);
question = jsonObject.getString("question");
idQuestion = jsonObject.getInt("idquestion");
questionInformation.add(question);
questionInformation.add(String.valueOf(idQuestion));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return questionInformation;
}
@Override
protected void onPostExecute(ArrayList<String> status) {
//loadingDialog.dismiss();
//findViewById(R.id.cb_fond).invalidate();
String question = questionInformation.get(0);
int idQuestion = Integer.valueOf(questionInformation.get(1));
tv_title_question.setText(question);
}
}
NextQuestionChooseAsync nqca = new NextQuestionChooseAsync();
nqca.execute();
}
如何解决我的问题?
非常感谢。
答案 0 :(得分:0)
将NextQuestionChooseAsync类放入其自己的文件中,并为要更改的视图创建私有字段,然后将它们从活动传递到构造函数中的NextQuestionChooseAsync。如果在asynctask期间销毁活动和textview,则使用WeakReference避免泄漏内存。
像这样:
NextQuestionChooseAsync class NextQuestionChooseAsync extends AsyncTask<String, Void, ArrayList<String>> {
private WeakReference<TextView> mTvTitleReference;
private Dialog loadingDialog;
public NextQuestionChooseAsync(TextView tvTitleTextView) {
super();
mTvTitleReference = new WeakReference<>(tvTitleTextView);
}
@Override
protected void onPostExecute(ArrayList<String> status) {
//loadingDialog.dismiss();
//findViewById(R.id.cb_fond).invalidate();
String question = questionInformation.get(0);
int idQuestion = Integer.valueOf(questionInformation.get(1));
if (mTvTitleReference.get() != null) {
mTvTitleReference.get().setText(question);
}
}
// the rest of your code
并在invokeNextQuestion()
private void invokeNextQuestion() {
NextQuestionChooseAsync nextquestionTask = new NextQuestionChooseAsync(tv_title_question);
nextquestionTask.execute(/**put your paramters here if needed**/);
}