我希望我在标题中正确解释了这个问题。我有一些内部类扩展 AsyncTask 来读取一些 JSON 数据并解析它。对于用户进行的每个搜索查询,我都有两个单独的查询。一旦我有了两个单独的结果,我想用相同的方法对它们做一些事情。我的想法是使用可变参数方法,并在每个单独的 AsyncTask 的 onPostExecute 中将结果发送到该方法。不确定这是否是正确的方法,因为应用程序在搜索时崩溃。我希望这是有道理的。谢谢你的帮助。如果需要,我会尽量使这一点更清楚。
package com.tot.tipofthetongue;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
public class Main extends Activity {
EditText searchOne;
EditText searchTwo;
Button findItems;
@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);
findItems = (Button) findViewById(R.id.findItems);
}
public void displayResults(View view){
//do something in response to button
//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();
Log.d("searchurltwo", searchURLTwo.toString());
new getIdOne().execute(searchURLOne);
new getIdTwo().execute(searchURLTwo);
}
public void myFirstID(String idOne){
final StringBuilder creditURLOne = new StringBuilder(buildCreditURL.getCreditURLOne(idOne));
String creditOne = creditURLOne.toString();
Log.d("idString", creditOne.toString());
new getFirstTitle().execute(creditOne);
}
public void mySecondID(String idTwo){
final StringBuilder creditURLTwo = new StringBuilder(buildCreditURL.getCreditURLTwo(idTwo));
String creditTwo = creditURLTwo.toString();
new getSecondTitle().execute(creditTwo);
}
public class getIdOne extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String TAG_ID = "id";
String TAG_RESULTS = "results";
String personOne = null;
String firstID = null;
JSONArray results = null;
JSONParser jParser = new JSONParser();
JSONObject jSon = jParser.getJSONFromUrl(personOne);
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
protected void onPostExecute(String result){
//make credit search url1
myFirstID(result);
}
}
public class getIdTwo extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String TAG_ID = "id";
String TAG_RESULTS = "results";
String personTwo = null;
String secondID = null;
JSONArray results = null;
JSONParser jParser = new JSONParser();
JSONObject jSon = jParser.getJSONFromUrl(personTwo);
try{
results = jSon.getJSONArray(TAG_RESULTS);
for(int i = 0; i < results.length(); i++){
JSONObject r = results.getJSONObject(i);
secondID = r.getString(TAG_ID);
}
}
catch(JSONException e){
Log.e("Error", e.toString());
}
return secondID;
}
@Override
protected void onPostExecute(String result){
//make credit search url1
mySecondID(result);
}
}
public class getFirstTitle extends AsyncTask<String, Void, ArrayList<String>>{
String TAG_CAST = "cast";
String TAG_TITLE = "title";
ArrayList<String> firstCast = new ArrayList<String>();
String title = null;
JSONArray castArray;
String creditURLOne = null;
JSONParser jParser = new JSONParser();
JSONObject jSon = jParser.getJSONFromUrl(creditURLOne);
@Override
protected ArrayList<String> doInBackground(String... creditOne) {
// TODO Auto-generated method stub
creditURLOne = creditOne.toString();
return getFirstJSONTitle(creditURLOne);
}
final ArrayList<String> getFirstJSONTitle(String creditURLOne){
Log.d("crediturl in title method has", castArray.toString());
try{
castArray = jSon.getJSONArray(TAG_CAST);
for(int i=0; i<castArray.length(); i++){
JSONObject f = castArray.getJSONObject(i);
title = f.getString(TAG_TITLE);
firstCast.add(title);
}
}
catch(JSONException e){
return null;
}
return firstCast;
}
@Override
protected void onPostExecute(ArrayList<String> result){
getFirstCast(firstCast);
}
}
public class getSecondTitle extends AsyncTask<String, Void, ArrayList<String>>{
String TAG_CAST = "cast";
String TAG_TITLE = "title";
ArrayList<String> secondCast = new ArrayList<String>();
String title = null;
JSONArray castArray;
String creditURLTwo = null;
JSONParser jParser = new JSONParser();
JSONObject jSon = jParser.getJSONFromUrl(creditURLTwo);
@Override
protected ArrayList<String> doInBackground(String... creditTwo) {
// TODO Auto-generated method stub
creditURLTwo = creditTwo.toString();
return getSecondJSONTitle(creditURLTwo);
}
final ArrayList<String> getSecondJSONTitle(String creditURLTwo){
Log.d("crediturl in title method has", castArray.toString());
try{
castArray = jSon.getJSONArray(TAG_CAST);
for(int i=0; i<castArray.length(); i++){
JSONObject f = castArray.getJSONObject(i);
title = f.getString(TAG_TITLE);
secondCast.add(title);
}
}
catch(JSONException e){
return null;
}
return secondCast;
}
@Override
protected void onPostExecute(ArrayList<String> result){
getSecondCast(secondCast);
}
}
public static ArrayList<String> getFirstCast(ArrayList<String> firstCast){
return firstCast;
}
public static ArrayList<String> getSecondCast(ArrayList<String> secondCast){
return secondCast;
}
public ArrayList<String> getItems(ArrayList<String> firstCast, ArrayList<String> secondCast){
if(firstCast.size() < secondCast.size()){
firstCast.retainAll(secondCast);
Intent intent = new Intent(this, DisplayResultsActivity.class);
intent.putStringArrayListExtra("myItems", firstCast);
startActivity(intent);
return null;
}
else{
secondCast.retainAll(firstCast);
Intent intent = new Intent(this, DisplayResultsActivity.class);
intent.putStringArrayListExtra("myItems", firstCast);
startActivity(intent);
return null;
}
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.totlayout, menu);
return true;
}
}
这是我的 logcat 错误
06-17 01:07:36.754: E/AndroidRuntime(821): FATAL EXCEPTION: AsyncTask #1
06-17 01:07:36.754: E/AndroidRuntime(821): java.lang.RuntimeException: An error occured while executing doInBackground()
06-17 01:07:36.754: E/AndroidRuntime(821): at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-17 01:07:36.754: E/AndroidRuntime(821): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-17 01:07:36.754: E/AndroidRuntime(821): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-17 01:07:36.754: E/AndroidRuntime(821): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-17 01:07:36.754: E/AndroidRuntime(821): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-17 01:07:36.754: E/AndroidRuntime(821): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-17 01:07:36.754: E/AndroidRuntime(821): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-17 01:07:36.754: E/AndroidRuntime(821): at java.lang.Thread.run(Thread.java:856)
06-17 01:07:36.754: E/AndroidRuntime(821): Caused by: java.lang.NullPointerException
06-17 01:07:36.754: E/AndroidRuntime(821): at java.net.URI.parseURI(URI.java:353)
06-17 01:07:36.754: E/AndroidRuntime(821): at java.net.URI.<init>(URI.java:204)
06-17 01:07:36.754: E/AndroidRuntime(821): at java.net.URI.create(URI.java:725)
06-17 01:07:36.754: E/AndroidRuntime(821): at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
06-17 01:07:36.754: E/AndroidRuntime(821): at com.tot.tipofthetongue.JSONParser.getJSONFromUrl(JSONParser.java:34)
06-17 01:07:36.754: E/AndroidRuntime(821): at com.tot.tipofthetongue.Main$getIdOne.doInBackground(Main.java:78)
06-17 01:07:36.754: E/AndroidRuntime(821): at com.tot.tipofthetongue.Main$getIdOne.doInBackground(Main.java:1)
06-17 01:07:36.754: E/AndroidRuntime(821): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-17 01:07:36.754: E/AndroidRuntime(821): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-17 01:07:36.754: E/AndroidRuntime(821): ... 4 more
06-17 01:07:36.774: W/dalvikvm(821): threadid=12: thread exiting with uncaught exception (group=0x40a71930)
06-17 01:07:36.774: I/Process(821): Sending signal. PID: 821 SIG: 9