我无法将数据从一个单独的类中的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){
}
}
答案 0 :(得分:2)
覆盖onPostExecute(String result)
课程中的方法getFirstID
:
@Override protected void onPostExecute(String result) {
// Invoke method on Main activity or do other action.
}
此方法由UI线程执行,可以更新视图。您可以通过在MainActivity
类的字段中将其设置为构造函数来添加对getFirstID
的引用。
希望它有所帮助!
答案 1 :(得分:2)
一种非常简单的方法就是这样做。
public class getFirstID extends AsyncTask<String, Void, Pair<String, String>> {
public static interface MyAsyncCallback {
/**
* Called when the AsyncTask is finished processing the url.
* @param url the url that the firstId was from
* @param firstId the id that was parsed out of the json object
*/
public void setFirstId(String url, String firstId);
}
private final MyAsyncCallback mCallback;
public getFirstID(MyAsyncCallback callback) {
mCallback = callback;
}
public Pair<String,String> doInBackground(String... params) {
String firstID = parseJsonForID(params); // I am not going to copy your alg into here.
return Pair.create(params[0], firstId);
}
public void onPostExecute(Pair<String,String> urlAndFirstId) {
mCallback.setFirstId(urlAndFirstId.first, urlAndFirstId.second);
}
}
public class Main extends Activity implements MyAsyncCallback {
....
public void setFirstId(String url, String firstID) {
// Example
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("firstId", firstId);
intent.putExtra("url", url);
startActivity(intent);
}
}
答案 2 :(得分:1)
您可以使用意图将内容发送到其他活动。
Intent intent = new Intent(this, YourActivity.class);
intent.putExtra("name", string);
了解here
或者您可以将AsyncTask类放在活动类中。
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get_data);
}
private class Download extends AsyncTask <Void, Void, String> {
protected String doInBackground(Void...params) {
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
}
}
}