0

Hello M 在我的 android 应用程序中使用 json 解析。M 使用 php 进行 Web 服务。M 从服务器获取微调器中的值。现在我想将特定的微调器 ID 发送回服务器如何发送它,请帮助我。纳克斯

我的代码

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

    }
}
4

2 回答 2

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) {}
});
于 2013-08-29T09:29:11.120 回答
0

从事件处理程序中获取您单击的微调器项目 IDOnItemSelectedListener并将其发送回服务器。对于发送,您可以使用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();
        }
于 2013-08-29T09:39:21.333 回答