0

How to append the URL with (key,value) pair before making a get request

  • i am passing restaurant name from the previous activity as Intents

  • I am storing that value in variable restaurant_name


Now how can i pass the value stored in variable restaurant_name to the url as a (Key,value) before i make a get request


Restaurantdesc.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");

<------------------- rest of code------------------------> }

3 Answers3

1

I would recommend this solution:

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

I hope I could help!

csikos.balint
  • 1,107
  • 2
  • 10
  • 25
  • `Uri.Builder b = Uri.parse(url); b.appendQueryParameter("Key", restuarant_name); ` and at doBackgroundAction()`HttpGet request = new HttpGet(b.build.toString());` – csikos.balint Aug 30 '13 at 13:01
0

Yes of course you can, like this:

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

and so on

UPDATE

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;

    }
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
0

You can add the key value pair to the 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;

And now pass this url to your get request..

I Referred this link

Community
  • 1
  • 1
Girish Thimmegowda
  • 2,109
  • 2
  • 14
  • 15