我尝试使用AsyncTask执行两个URL同时获取一些数据。但是他们两个人同时只有一个偏见。我怎么解决它。 两个url都有标记值。使用标记值我从网址获取数据。
public class Manager extends Activity implements OnClickListener,
OnItemSelectedListener {
private Spinner area_wise_sp,
lineman_wise_sp;
ProgressBar pbar;
List<NameValuePair> params;
String url_add_cus = "http://10.0.2.2/api/android/add_cab.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.money_manager);
pbar = (ProgressBar) findViewById(R.id.progressBar1);
params = new ArrayList<NameValuePair>();
// execute two url with different tag value
params.add(new BasicNameValuePair("tag", "area_list"));
params.add(new BasicNameValuePair("opId", saveOpid));
params.add(new BasicNameValuePair("company", saveCompany));
Toast.makeText(getBaseContext(), params.toString(),
Toast.LENGTH_SHORT).show();
new ReadJSONResult().execute(url_add_cus);
params.add(new BasicNameValuePair("tag", "pack_list"));
params.add(new BasicNameValuePair("opId", saveOpid));
params.add(new BasicNameValuePair("company", saveCompany));
Toast.makeText(getBaseContext(), params.toString(),
Toast.LENGTH_SHORT).show();
new ReadJSONResult().execute(url_add_cus);
}
}
}
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = httpClient.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
} else {
Log.d("JSON", "Failed to download file");
Toast.makeText(getBaseContext(), "failed json",
Toast.LENGTH_SHORT).show();
pbar.setVisibility(View.VISIBLE);
}
} catch (Exception e) {
// Log.d("readJSONFeed", e.getLocalizedMessage());
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT)
.show();
pbar.setVisibility(View.VISIBLE);
}
return stringBuilder.toString();
}
class ReadJSONResult extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// TODO Auto-generated method stub
return readJSONFeed(urls[0]);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
pbar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
try {
JSONObject jsonObject = new JSONObject(result);
Dialog d = new Dialog(MoneyManager.this);
d.setTitle("result");
TextView tv = new TextView(MoneyManager.this);
tv.setText(result.toString());
d.setContentView(tv);
d.show();
pbar.setVisibility(View.GONE);
if ("pack_list".matches((jsonObject.getString("tag")))) {
// listval_for_pack.clear();
Toast.makeText(getBaseContext(), "packlist done",
Toast.LENGTH_LONG).show();
}
} else if ("area_list".matches((jsonObject.getString("tag")))) {
Toast.makeText(getBaseContext(), "arealist done",
Toast.LENGTH_LONG).show();
}
}
pbar.setVisibility(View.INVISIBLE);
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(),
Toast.LENGTH_LONG).show();
pbar.setVisibility(View.INVISIBLE);
}
}
}
}
答案 0 :(得分:0)
试试这个逻辑。我修改了它。希望它会对你有所帮助。
// in onPostExecute
else if ("area_list".matches((jsonObject.getString("tag")))) {
Toast.makeText(getBaseContext(), "arealist done",
Toast.LENGTH_LONG).show();
params.add(new BasicNameValuePair("tag", "pack_list"));
params.add(new BasicNameValuePair("opId", saveOpid));
params.add(new BasicNameValuePair("company", saveCompany));
Toast.makeText(getBaseContext(), params.toString(),
Toast.LENGTH_SHORT).show();
new ReadJSONResult().execute(url_add_cus);
}