简体   繁体   中英

How to set a text from my AsyncTask to a textview in a fragment?

Sooo here is my OnCreateView code

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_profile, container, false);
    tvMemberName = (TextView) v.findViewById(R.id.member_name);
    UrlPostHelper uph = new UrlPostHelper();
    uph.execute();
    return v;
}

and AsyncTask

    private class UrlPostHelper extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... params) {
        String url = "http://localhost:8080/MP/Profile";
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse response;
        String data = "no response";
        try {
            response = httpClient.execute(httpGet);
            data = EntityUtils.toString(response.getEntity());
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String s = "yow";


        try {
            JSONArray ja = new JSONArray(data);
            for (int i = 0 ; i < ja.length(); i++ ){
                JSONObject j = ja.getJSONObject(i);
                String firstName = j.getString("firstName");
                String lastName = j.getString("lastName");
                System.out.println(firstName);
                s = firstName +" " + lastName;
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return s;

    }

    protected void onPostExecute(String result){
        super.onPostExecute(result);
        Log.i("TAG", result );
        tvMemberName.setText(result);
    }


}

My problem is that in my android app it does not show anything at first but after a while like around 1-2 minutes the "yow" will show. It is not replaced with the member name though. I tried running my servlet and it works just fine. Please help me.

  • AsyncTask<Params, Progress, Result> .

     private class UrlPostHelper extends AsyncTask<Void, Void, String>{ private EditText tvMemberName; public UrlPostHelper(EditText tv){ this.tvMemberName = tv; } 
  • Return Text after background task finished.

     @Override protected String doInBackground(Void... params) { HttpResponse response; String data = "no response"; try { response = httpClient.execute(httpGet); data = getJSONString(response.getEntity()); } catch (Exception e) { e.printStackTrace(); } return data; //Text to set TvMember Value } 
  • Get String from response Util.

     public String getJSONString(InputStream is) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(is)); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); } 
  • Set EditText onPostExecute.

     protected void onPostExecute(String result){ Log.i("TAG", result ); tvMemberName.setText(result); } 
  • Pass EditText with the UrlPostHelper call.

     new UrlPostHelper(tvMemberName).excute(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM