我使用 Asynctask
从 php 加载和获取数据。而且我必须将 2 个参数传递给 php。
但我不知道怎么办。
这是Java代码:
public class info extends Activity{
ProgressDialog pDialog;
TextView movie_tittle, studio, date;
int std;
String movie, reservation, ttl, dt;
private String URL_CATEGORIES = "http://10.0.2.2/cinemainfo/info.php";
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> accountsList;
JSONArray accounts = null;
private static final String TAG_SUCCESS = "success";
private static final String TAG_ACCOUNT = "message";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
movie = getIntent().getStringExtra("kode_intent");
reservation = getIntent().getStringExtra("kode_intent2");
movie_tittle=(TextView)findViewById(R.id.tv_tittle);
date=(TextView)findViewById(R.id.tv_date);
studio=(TextView)findViewById(R.id.tv_studio);
new GetCategories().execute();
}
private class GetCategories extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(info.this);
pDialog.setMessage("Please Wait..");
pDialog.setCancelable(false);
pDialog.show();
}
protected Void doInBackground(Void... arg0) {
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("id_movie", movie));
params.add(new BasicNameValuePair("id_reservation", reservation));
JSONObject json = jParser.makeHttpRequest(URL_CATEGORIES, "GET", params);
Log.d("All Accounts: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
accounts = json.getJSONArray(TAG_ACCOUNT);
for (int i = 0; i < accounts.length(); i++) {
JSONObject json_data = accounts.getJSONObject(i);
ttl=json_data.getString("movie_tittle");
dt=json_data.getString("date");
std = json_data.getInt("studio");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
result();
}
}
private void result() {
try{
movie_tittle.setText(ttl);
date.setText(dt);
studio.setText(String.valueOf(std));
}
catch(Exception e){
Log.e("log_tag","Error in Display!" + e.toString());;
}
}
}
我想将 id_movie
和 id_reservation
传递给 php 代码。两者都是从 movie = getIntent().getStringExtra("kode_intent");
和 reservation = getIntent().getStringExtra("kode_intent2");
但是当我在模拟器中运行代码时,它什么都不显示。php 代码很好。但是我不确定我的 java 代码。如何在 doInBackground
异步任务中传递 2 个参数?我做错什么了吗 ?
最佳答案
String curloc = current.toString();
String itemdesc = item.mDescription;
ArrayList<String> passing = new ArrayList<String>();
passing.add(itemdesc);
passing.add(curloc);
new calc_stanica().execute(passing); //no need to pass in result list
And change your async task implementation
public class calc_stanica extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(baraj_mapa.this);
dialog.setTitle("Calculating...");
dialog.setMessage("Please wait...");
dialog.setIndeterminate(true);
dialog.show();
}
protected ArrayList<String> doInBackground(ArrayList<String>... passing) {
ArrayList<String> result = new ArrayList<String>();
ArrayList<String> passed = passing[0]; //get passed arraylist
//Some calculations...
return result; //return result
}
protected void onPostExecute(ArrayList<String> result) {
dialog.dismiss();
String minim = result.get(0);
int min = Integer.parseInt(minim);
String glons = result.get(1);
String glats = result.get(2);
double glon = Double.parseDouble(glons);
double glat = Double.parseDouble(glats);
GeoPoint g = new GeoPoint(glon, glat);
String korisni_linii = result.get(3);
}
关于java - 如何在 doInBackground asynctask 中放入 2 个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21423537/