Hello M在我的android应用程序中使用json解析。 M使用php进行Web服务。 M从服务器获取微调器中的值。现在我想将特定的微调器ID发送回服务器如何发送它请帮助我。 Thnaks
我的代码
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 :(得分: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) {}
});
答案 1 :(得分:0)
从OnItemSelectedListener
事件处理程序&amp;获取您点击的微调项目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();
}