Im trying to pass an object from doInBackground to OnPostExecute but failed. I have read similar topics and find them not relevant. (Maybe im not that experienced enough) I'm new to android would like some pointers. I will paste relevant codes instead of everything for easier readability.
Main Activity
public void processFinish(Question q) {
TextView tview;
tview = (TextView)findViewById(R.id.textView1);
tview.setText(q.getQuestion());
}
AsyncTask (Q is an object)
public class getQus extends AsyncTask<String,Void,Question> {
public AsyncResponse delegate;
@Override
protected Question doInBackground(String... params) {
String range = "1";
// TODO Auto-generated method stub
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("range",range));
int q_id = 0;
String result=null;
String q_qus =" ";
String result2 = " ";
Question q = new Question();
InputStream is = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.6/fyp/qus.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result2=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
System.out.println("HEllo");
//parse json data
try{
JSONArray jArray = new JSONArray(result2);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("q_id")+
", qus: "+json_data.getString("q_qus")
);
q.setQid(json_data.getInt("q_id"));
q.setQuestion(json_data.getString("q_qus"));
return q;
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return q;
}
protected void onPostExecute(Question q) {
System.out.println("AT ONPOSTEXECUTE");
System.out.println(q.getQuestion());
delegate.processFinish(q);
}
}
Findings: I am able to see my log.i results in my console only. OnPostExecute does not fire at all.
Any Kind Help Or Suggestions would be deeply appreciated. Thank you very much!