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

on Saturday, August 2, 2014


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.


0 comments:

Post a Comment