我无法从单独的类文件中的异步任务中将数据返回到我的主要活动中。
这是我的主要活动:
public class Main extends Activity {
EditText searchOne;
EditText searchTwo;
Button findMovies;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.totlayout);
//set the UI elements
searchOne = (EditText) findViewById(R.id.searchOne);
searchTwo = (EditText) findViewById(R.id.searchTwo);
findMovies = (Button) findViewById(R.id.findMovies);
}
public void displayResults(View view){
//do something in response to button
Intent intent = new Intent(this, DisplayResultsActivity.class);
//make person search url1
final StringBuilder personSearchURLOne = new StringBuilder(getName.getName1(searchOne));
final String searchURLOne = personSearchURLOne.toString();
Log.d("searchurlone", searchURLOne.toString());
//make person search url2
final StringBuilder personSearchURLTwo = new StringBuilder(getName.getName2(searchTwo));
final String searchURLTwo = personSearchURLTwo.toString();
//get ID 1 this needs to get data back from asynctask in the type string to be passed to buildCreditURL
new getFirstID().execute(searchURLOne);
//get ID 2
new getFirstID().execute(searchURLTwo);
//make credit search url1
final StringBuilder creditURLOne = new StringBuilder(buildCreditURL.getCreditURLOne(firstID));
这是包含我的 AsyncTask 的单独类文件:
package com.tot.tipofthetongue;
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class getFirstID extends AsyncTask<String, Void, String> {
public static String TAG_ID = "id";
public static String TAG_RESULTS = "results";
String personOne = null;
static String firstID = null;
@Override
protected String doInBackground(String... personSearchURLOne) {
Log.d("personSearchURLOne in getFirstID containts", personSearchURLOne[0]);
JSONArray results = null;
JSONParser jParser = new JSONParser();
JSONObject jSon = jParser.doInBackground(personSearchURLOne[0]);
try{
results = jSon.getJSONArray(TAG_RESULTS);
for(int i = 0; i < results.length(); i++){
JSONObject r = results.getJSONObject(i);
firstID = r.getString(TAG_ID);
}
}
catch(JSONException e){
Log.e("Error", e.toString());
}
/need this value as string to be passed back to the main activity
return firstID;
}
}
如何将 firstID 作为字符串输出到我的主要活动中,以便可以在属于另一个单独的类文件的另一个方法中使用它。
更新的 getFirstID:
package com.tot.tipofthetongue;
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class getFirstID extends AsyncTask<String, Void, String> {
public static String TAG_ID = "id";
public static String TAG_RESULTS = "results";
String personOne = null;
static String firstID = null;
@Override
protected String doInBackground(String... personSearchURLOne) {
Log.d("personSearchURLOne in getFirstID containts", personSearchURLOne[0]);
JSONArray results = null;
JSONParser jParser = new JSONParser();
JSONObject jSon = jParser.doInBackground(personSearchURLOne[0]);
try{
results = jSon.getJSONArray(TAG_RESULTS);
for(int i = 0; i < results.length(); i++){
JSONObject r = results.getJSONObject(i);
firstID = r.getString(TAG_ID);
}
}
catch(JSONException e){
Log.e("Error", e.toString());
}
return firstID;
}
@Override
public void onPostExecute(String firstID){
}
}