2014-01-29 2 views
1

Я использую Asynctask для загрузки и получения данных с php. И мне нужно передать 2 параметра в php. Но я не знаю как.Как поставить 2 параметра в doInBackground asynctask?

Вот код 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 code..Both получает от movie = getIntent().getStringExtra("kode_intent"); и reservation = getIntent().getStringExtra("kode_intent2");

Но когда я запускаю код в эмуляторе, он ничего не отображает. PHP-код в порядке. Но я не уверен в моем java-коде. Как передать 2 параметра в doInBackground asynctask? Я сделал что-то не так ?

+0

Печать кино и резервирование значения в LogCat и дайте мне знать, –

+0

Значения для кино = 1 и ресервировании = 17 .. Когда я пытаюсь изменить код, не используя 'Asynctask', но используя этот 'ArrayList postParameters = new ArrayList (); \t \t postParameters.add (новый BasicNameValuePair ("id_movie", фильм)); \t \t postParameters.add (новый BasicNameValuePair («id_reservation», резервирование)); 'it works .. – Aprilia

+0

Но я действительно хочу использовать' asynctask' для загрузки данных .. :( – Aprilia

ответ

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); 
    } 
2

Призвание:

String[] arrayOfValue = new String[2]; 
arrayOfValue[0] = movie; 
arrayOfValue[1] = reservation; 

new GetCategories().execute(arrayOfValue); 

Использование:

protected ArrayList<String> doInBackground(String... passing){ 
String movie = passing[0]; 
String reservation = passing[1]; 
} 
Смежные вопросы