Q:

Modify layout elements when execute http request

I will explain my problem.
I have an activity which retrieves data from a previous activity. When I click on my button, the method invokeSendQuestionChoose()is executed.
This method does two actions: send http request on two different ressources.
The first resource is /nextquestion. I want to refresh my Checkbox and TextView with the new recovered informations. 
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);

    ...
}

Method when click on button:
public void invokeSendQuestionChoose(View view){

    invokeSendQuestionChoose();
    invokeNextQuestion();

}

Method http request:
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();
}

How can I resolve my problem? 
Thanks a lot.

A:

Put your NextQuestionChooseAsync class in it's own file and create private fields to the views you want to change and then pass them from the activity to the NextQuestionChooseAsync in the constructor. Using WeakReference avoid leak memory if the activity and the textview are destroyed during the asynctask.
Like so :
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

and in your invokeNextQuestion()
private void invokeNextQuestion() {
    NextQuestionChooseAsync nextquestionTask = new NextQuestionChooseAsync(tv_title_question);
    nextquestionTask.execute(/**put your paramters here if needed**/);
}

