我無法從單獨的類中的asynctask中將數據返回到我的主要活動中。將數據從AsyncTask返回到活動
這是我的主要活動:
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){
}
}
所以在我的getFirstID類中添加時:Inent intent = new Intent(this,Main.class);在doInBackground下我得到一個構造函數未定義的錯誤。 – user881667