【发布时间】:2017-12-04 16:20:06
【问题描述】:
注意:我在术语方面做得不是很好,因为这是我第一次接触面向对象的编程和 android studio。
在 android studio 中,我为 JSON 获取任务创建了一个类。 (我不包括后台覆盖中的 do)。
public class jsonTasks extends AsyncTask<String, String, String> {
public interface AsyncResponse {
void processFinish(String output);
}
private AsyncResponse delegate = null;
jsonTasks(AsyncResponse delegate){
this.delegate = delegate;
}
@Override
protected void onPostExecute(String result) {
delegate.processFinish(result);
}
现在在我的活动中:
public class activity extends Activity {
// in here I've declared the string variable
String valuableInfo;
// FIRST TASK
jsonTasks asyncTask = new jsonTasks(new jsonTasks.AsyncResponse() {
@Override
public void processFinish(String output) {
String TAG = activity.class.getSimpleName();
JSONObject info;
//i do stuff with the json here
//i set the textboxes normally
txtSumName.setText(sumInfo.getString("name"));
//but setting the string valuableInfo is not working
valuableInfo = "info I took from json operation"
});
// and here I execute
asyncTask.execute("url");
// SECOND TASK
jsonTasks asyncTask2 = new .... {
.................
}
asyncTask2.execute("https://example.com/" + valuableInfo);
}
我想使用该变量的地方是另一个“asyncTask2”,它使用 URL 上的有价值信息继续获取。
当我运行第二个“asyncTask2”时,该变量始终为空。
【问题讨论】:
标签: java android json variables android-asynctask