将对象从AsyncTask doInBackground传递给OnPostExecute

时间:2014-12-19 15:01:12

标签: android android-asynctask

我试图将一个对象从doInBackground传递给OnPostExecute但是失败了。 我读过类似的主题,发现它们不相关。 (也许我不够经验) 我对android的新手想要一些指针。 我会粘贴相关代码而不是所有内容,以便于阅读。

主要活动

public void processFinish(Question q) {
    TextView tview;
    tview = (TextView)findViewById(R.id.textView1);
    tview.setText(q.getQuestion());
}

AsyncTask(Q是一个对象)

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);
}

}

调查结果: 我只能在我的控制台中看到我的log.i结果。 OnPostExecute根本不会触发。

任何帮助或建议都将深表感谢。 非常感谢你!

1 个答案:

答案 0 :(得分:2)

更改

 extends AsyncTask<String,Void,Object> 

 extends AsyncTask<String,Void,Question> 

第三个通用参数是Result。来自doc

  

结果,后台计算结果的类型。

对于NPE:

public class getQus extends AsyncTask<String,Void,Question> {
public AsyncResponse delegate;
   public getQus(AsyncResponse d) {
       delegate = d;
   }
   // other code
}

如果您的活动正在实施该界面,您只需执行以下操作:

new getQus(ActivityName.this).execute();