在 class 方法中设置要在后台(异步)任务中访问的变量 class (Java/Android)
Set a variable in a class method to be accessed in a background (async) task class (Java/Android)
我正在尝试设置一个变量 'election_id' 从一个意图中获取额外的 class。我需要使用 id 将其解析为 PHP URL 以便检索一些 JSON。我想要做的是设置一个变量,使其等于先前 class 中分配的选举 ID,以用于后台任务:
public class AdminFinishCandidateActivity extends AppCompatActivity {
String json_string;
String json_string_result;
String election_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_finish_candidate);
System.out.println("creating");
Bundle electionData = getIntent().getExtras();
if (electionData==null) {
return;
}
election_id = electionData.getString("electionId");
}
public void getJson(View view){
new BackgroundTask().execute();
}
class BackgroundTask extends AsyncTask<Void,Void,String> {
String json_url="http://192.168.56.1/dan/db/getjson.php?election_id=" + election_id;
// code to retrieve JSON
}
}
}
目前,election_id 正在我的 onCreate 方法中分配,但是我知道我无法从我的 BackgroundTask class 中的此方法访问它,我在其中定义了字符串 json_url.如何才能在这个 class 中调用 election_id 变量?检索 intent extra 的方法在此 class 中不起作用。当我 运行 代码与 JSON 代码一样时,没有检索到任何内容。谢谢
您可以按如下方式将字符串传递给 AsyncTask:
public class BackgroundTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
String json_url="http://192.168.56.1/dan/db/getjson.php?election_id=" + params[0];
//perform network operation and return response
return response;
}
@Override
protected void onPostExecute(String res){
//Batman Rocks
}
}
现在执行任务如下:
new BackgroundTask().execute(election_id);
我正在尝试设置一个变量 'election_id' 从一个意图中获取额外的 class。我需要使用 id 将其解析为 PHP URL 以便检索一些 JSON。我想要做的是设置一个变量,使其等于先前 class 中分配的选举 ID,以用于后台任务:
public class AdminFinishCandidateActivity extends AppCompatActivity {
String json_string;
String json_string_result;
String election_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_finish_candidate);
System.out.println("creating");
Bundle electionData = getIntent().getExtras();
if (electionData==null) {
return;
}
election_id = electionData.getString("electionId");
}
public void getJson(View view){
new BackgroundTask().execute();
}
class BackgroundTask extends AsyncTask<Void,Void,String> {
String json_url="http://192.168.56.1/dan/db/getjson.php?election_id=" + election_id;
// code to retrieve JSON
}
}
}
目前,election_id 正在我的 onCreate 方法中分配,但是我知道我无法从我的 BackgroundTask class 中的此方法访问它,我在其中定义了字符串 json_url.如何才能在这个 class 中调用 election_id 变量?检索 intent extra 的方法在此 class 中不起作用。当我 运行 代码与 JSON 代码一样时,没有检索到任何内容。谢谢
您可以按如下方式将字符串传递给 AsyncTask:
public class BackgroundTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
String json_url="http://192.168.56.1/dan/db/getjson.php?election_id=" + params[0];
//perform network operation and return response
return response;
}
@Override
protected void onPostExecute(String res){
//Batman Rocks
}
}
现在执行任务如下:
new BackgroundTask().execute(election_id);