0

如何在发出获取请求之前使用 (key,value) 对附加 URL

  • restaurant name从以前的活动中 经过Intents

  • 我将该值存储在变量中restaurant_name


现在如何在发出获取请求之前将存储在变量restaurant_name中的值url作为(键,值)传递给


餐厅desc.java

public class RestaurantDesc extends Activity{
    // url to make request


    private static String url = "http://***********************";


    String restaurant_name ;
    ProgressDialog progressDialog;
    JSONObject jsonObject;
    JSONArray first_array ;
    JSONArray second_array;
    TextView textView;
    TextView text;
    TextView topdisp;

    private SparseArray<String> startarsMap = new SparseArray<String>();
    private SparseArray<String> saladsMap = new SparseArray<String>();
    private SparseArray<String> maincourseMap = new SparseArray<String>();
    private SparseArray<String> desertMap = new SparseArray<String>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.restaurant_desc);

        //url = getIntent().getStringExtra("key");
        restaurant_name = getIntent().getStringExtra("key");



        topdisp=(TextView) findViewById(R.id.TopNavigationBarRestaurantDescActivityName);
        Button BACKBUTTON=(Button) findViewById(R.id.TopNavigationBarRestaurantDescActivityBackButton);
        BACKBUTTON.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent emp1=new Intent(RestaurantDesc.this,MainActivity.class);
                startActivity(emp1);
            }
        });


        Button PHOTOBUTTON=(Button) findViewById(R.id.RestaurantPhotosButton);
        PHOTOBUTTON.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent pht=new Intent(RestaurantDesc.this,RestaurantPhotos.class);
                pht.putExtra("key", getIntent().getStringExtra("key"));

                startActivity(pht); 
            }
        });



        progressDialog=new ProgressDialog(RestaurantDesc.this);
        new ParsingAsync().execute();
    }




    private class ParsingAsync extends AsyncTask<Void, Void, Void>
    {



        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog=ProgressDialog.show(RestaurantDesc.this, "", "Please Wait", true, false);





        }



        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            String _response = null;
            try
            {
                HttpClient httpclient = new DefaultHttpClient();
                httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

                //url = url."?Key=restaurant_name";
                //url += "?" + "key=" + restaurant_name;


                HttpGet request = new HttpGet(url=url += "?" + "Key=" + restaurant_name);
                HttpResponse response = httpclient.execute(request);
                HttpEntity resEntity = response.getEntity();
                _response =EntityUtils.toString(resEntity);

                jsonObject = new JSONObject(_response);
                first_array = jsonObject.getJSONArray("RestaurantNAME");

<-------------------- 其余代码------------------------> }

4

3 回答 3

1

我会推荐这个解决方案:

Uri.Builder b = Uri.parse("http://www.yoursite.com:12345").buildUpon();

b.path("/path/to/something/");
b.appendQueryParameter("arg1", String.valueOf(42));

if (username != "") {
  b.appendQueryParameter("username", username);
}

String url = b.build().toString();

我希望我能帮上忙!

于 2013-08-30T09:44:12.293 回答
0

是的,你当然可以,像这样:

 http://54.218.73.244:7004/RestaurantDesc?keyname1=value1&keyname2=value2&keyname3=value3...

等等

更新

private static String url = "http://54.218.73.244:7004/RestaurantDesc";
    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        String _response = null;
        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

            //url = url."?Key=restaurant_name";
            HttpGet request = new HttpGet(url+"?Key="+restaurant_name);
            HttpResponse response = httpclient.execute(request);
            HttpEntity resEntity = response.getEntity();
            _response =EntityUtils.toString(resEntity);

            jsonObject = new JSONObject(_response);
            first_array = jsonObject.getJSONArray("RestaurantNAME");


        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }
于 2013-08-30T09:46:19.307 回答
0

您可以将键值对添加到url

if(!url.endsWith("?"))
    url += "?";

List<NameValuePair> params = new LinkedList<NameValuePair>();

params.add(new BasicNameValuePair("Key1", "value1"));
params.add(new BasicNameValuePair("Key2", "value2"));

String paramString = URLEncodedUtils.format(params, "utf-8");

url += paramString;

现在将其传递url给您的获取请求..

我参考了这个链接

于 2013-08-30T12:03:34.777 回答