2013-08-29 84 views
0

你好M使用JSON解析在我的Android應用程序。 M使用PHP進行Web服務。 M從服務器獲取微調器中的值。現在我想發回特定的微調號回到服務器如何發送請幫助我。 Thnaks如何發送微調ID到服務器使用JSON Web服務在Android

我的代碼

class GetProviderName extends AsyncTask<String, Void, Boolean> 
{ 


    protected void onPreExecute() 
    { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 

     pd2=ProgressDialog.show(NewSales.this, "Loading", "Please wait"); 
    } 


    @Override 
    protected Boolean doInBackground(String... params) { 
     // TODO Auto-generated method stub 





      try 
      { 
       HttpClient httpclient = new DefaultHttpClient(); 

       HttpGet httppost = new HttpGet(params[0]); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       view_rates = EntityUtils.toString(entity, HTTP.UTF_8); 
       System.out.println(view_rates); 





       JSONArray jarray=new JSONArray(view_rates); 
       System.out.println("Jarray Length:"+jarray.length()); 

     if(jarray.length()==0) 
     { 
      norecord=true; 
     } 
     else 
     { 
      for(int i=0;i<jarray.length();i++) 
      { 
       JSONObject jbnew=jarray.getJSONObject(i); 

       int provider_id=Integer.valueOf(jbnew.getString("voucher_provider_id")); 

       String voucher_provider_name=jbnew.getString("voucher_provider_name"); 
       Provider_structuer provider_dt=new Provider_structuer(); 
       provider_dt.setProvider_id(provider_id); 
       provider_dt.setProvider_name(voucher_provider_name); 
       all_provider_val.add(provider_dt); 
       provider_names.add(voucher_provider_name); 


       //jbnew.put("voucher_provider_id", provider_shop_names.getSelectedItem().toString()); 

      } 
     } 
     System.out.println("provider names" +provider_names.size()); 
     System.out.println("provider nshpos" +all_provider_val.size()); 



     return true; 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
     return false; 
    } 
} 



    @Override 
    protected void onPostExecute(Boolean result) 
    { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     pd2.dismiss(); 
     if(result) 
     { 
      ArrayAdapter<String>myadapter=new ArrayAdapter<String>(NewSales.this,android.R.layout.simple_spinner_dropdown_item,provider_names); 
      provider_shop_names.setAdapter(myadapter); 
     } 
     else 
     { 
      provider_shop_names.setAdapter(null); 
      Toast.makeText(NewSales.this,"Sorry problem with connection.",Toast.LENGTH_LONG).show(); 
     } 

    } 
} 

回答

0

如果provider_names持有的名稱和all_provider_val相應的值,你可以在你使用的微調setOnItemSelectedListener。您可以獲取所選項目的位置。然後使用該位置從all_provider_val列表中獲取值。類似的東西:

ArrayAdapter<String>myadapter=new ArrayAdapter<String>(NewSales.this,android.R.layout.simple_spinner_dropdown_item,provider_names); 
provider_shop_names.setAdapter(myadapter); 

provider_shop_names.setOnItemSelectedListener(new OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id){ 
     String selectedValue = all_provider_val.get(position); 
     //then return selectedValue to the web service 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parentView) {} 
}); 
0

得到OnItemSelectedListener事件處理&您點擊微調項ID發送回服務器。發送時,您可以使用HTTP POST。下面是使用發送HTTP POST數據到服務器的代碼示例:

// http post 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(urlStr); 
      httppost.setEntity(new UrlEncodedFormEntity(orderJson)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

     } catch (Exception e) { 
     } 

     // convert response to string 
     try { 
      BufferedReader reader = new BufferedReader(
        new InputStreamReader(is, "iso-8859-1"), 8); 
      sb = new StringBuilder(); 
      sb.append(reader.readLine() + "\n"); 
      String line = "0"; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      // response 
      result = sb.toString(); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "Order not submitted!", 
        Toast.LENGTH_SHORT).show(); 
     } 
+0

是什麼orderJson在我們的代碼plz幫助我 – user2306201

+0

「orderJson」是我要發送到服務器的內容。 'List orderJson = new ArrayList (); \t \t \t orderJson.add(new BasicNameValuePair(「order」,json)); ' 用您的微調ID替換「json」。 –

相關問題