gpt4 book ai didi

java - 如何设置 doInbackground 在继续 mainActivity 之前完成其任务?

转载 作者:行者123 更新时间:2023-11-29 12:04:27 24 4
gpt4 key购买 nike

这些是我的MainActivity :

    database_connector wp_terms = new database_connector("SELECT * FROM  `dse120071750`.`wp_terms` ",progressDialog,this);
wp_terms.execute();
wp_terms.onPreExecute();
try {

for (int i=0; i<wp_terms.getJsonArray().length(); i++){
JSONObject obj = wp_terms.getJsonArray().getJSONObject(i);
this.wp_terms.put(obj.getString("term_id"), obj.getString("name"));
}
} catch (JSONException e) {
e.printStackTrace();
}

数据库连接器:

package hk.hoome.www.mobilehoome;

public class database_connector extends AsyncTask<Void,Void, Void> {

//String mode;
HttpResponse response;
String sql;
JSONArray jsonArray;
searchPage searchPage;

public database_connector(String sql, searchPage searchPage){
//this.mode = mode;
this.sql = sql;
this.searchPage = searchPage;
jsonArray = new JSONArray();
}

@Override
protected Void doInBackground(Void... params) {
connect();
publishProgress();
return null;
}



@Override
protected void onProgressUpdate(Void... values) {

}

public void connect() {
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
nameValuePair.add(new BasicNameValuePair("sql", sql));
//nameValuePair.add(new BasicNameValuePair("mode", mode));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.hoome.hk/hoomeMobileApps/connectDB.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response ", entityResponse.substring(2));
jsonArray = new JSONArray(entityResponse.substring(2));

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}



public JSONArray getJsonArray(){
return jsonArray;
}

}

当我运行此代码时,for (int i=0; i<wp_terms.getJsonArray().length(); i++){这导致 nullPointerException

我相信这是因为doInbackground尚未完成其过程,但 mainActivity继续运行。我该如何设置doInbackground在继续运行 mainActivity 之前必须完成吗?

解决方案?

   try {
while (wp_posts.getJsonArray().equals(null))
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}

这是一个好的解决方案吗?

最佳答案

在数据库连接器类中使用回调并传递完成回调。为了简单起见,我使用 Runnable 接口(interface)。一般来说,尝试拥有自己的接口(interface),并通过自定义接口(interface)在后台线程和主线程之间传递参数。

package hk.hoome.www.mobilehoome;

public class database_connector extends AsyncTask<Void,Void, Void> {

//String mode;
HttpResponse response;
String sql;
JSONArray jsonArray;
searchPage searchPage;
private Runnable activityCallback;

public void setCallback(Runnable callback) {
this.activityCallback = callback;
}

public database_connector(String sql, searchPage searchPage){
//this.mode = mode;
this.sql = sql;
this.searchPage = searchPage;
jsonArray = new JSONArray();
}

@Override
protected Void doInBackground(Void... params) {
connect();
publishProgress();
return null;
}

protected void onPostExecute(Void result) {

if(activityCallback != null) {
activityCallback.run();
}
}


@Override
protected void onProgressUpdate(Void... values) {

}

public void connect() {
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
nameValuePair.add(new BasicNameValuePair("sql", sql));
//nameValuePair.add(new BasicNameValuePair("mode", mode));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.hoome.hk/hoomeMobileApps/connectDB.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response ", entityResponse.substring(2));
jsonArray = new JSONArray(entityResponse.substring(2));

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}



public JSONArray getJsonArray(){
return jsonArray;
}
}

在你的MainActivity中

database_connector wp_terms = new database_connector("SELECT * FROM  `dse120071750`.`wp_terms` ",progressDialog,this);
wp_terms.setCallback(new Runnable() {

public void run() {

try {

for (int i=0; i<wp_terms.getJsonArray().length(); i++){
JSONObject obj = wp_terms.getJsonArray().getJSONObject(i);
this.wp_terms.put(obj.getString("term_id"), obj.getString("name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
wp_terms.execute();

关于java - 如何设置 doInbackground 在继续 mainActivity 之前完成其任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31781190/

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