gpt4 book ai didi

android - 将对象从 AsyncTask doInBackground 传递到 OnPostExecute

转载 作者:行者123 更新时间:2023-11-29 00:14:44 28 4
gpt4 key购买 nike

我试图将对象从 do​​InBackground 传递到 OnPostExecute 但失败了。我读过类似的主题,但发现它们不相关。 (可能我经验不够)我是 android 的新手,希望得到一些指导。为了便于阅读,我将粘贴相关代码而不是所有内容。

主要 Activity

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 根本不会触发。

如有任何帮助或建议,我们将不胜感激。非常感谢!

最佳答案

改变

 extends AsyncTask<String,Void,Object> 

 extends AsyncTask<String,Void,Question> 

第三个通用参数是结果。来自文档

Result, the type of the result of the background computation.

对于 NPE:

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

如果您的 Activity 正在实现该接口(interface),您只需执行以下操作:

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

关于android - 将对象从 AsyncTask doInBackground 传递到 OnPostExecute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27568610/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com