Search code examples
androidjsonapimultipartentity

How to pass half parameters in requestUrl and other in JSON request in android?


I am new to android,I have made an activity in that i have to post some parameters to make api call and get response,I have to pass some parameters appending to request url and others as in Json format,Please tell me how can i do,My sample url request is as below:

http://dev.abctest.com/api/v1/book?customer_firstname=jigar&customer_lastname=jims&customer_mobile=9033309333&[email protected]&source_country=India&number_of_travellers=15

and other parameters in json body like below:

{

    "destinations": [
        {
            "city_id": 1,
            "start_date": "2014/08/28",
            "end_date": "2014/09/30"
        },
        {
            "city_id": 5,
            "start_date": "2014/08/10",
            "end_date": "2014/09/03"
        }
    ]
}

Solution

  • First you need to append the url fields to your base url. Then you can add the optional fields if you have any. Then the your data as an entity in HttpPost where the url will be the one obtained after processing.

    Try following :

    1. The parent method to be called.

      public void request(String baseUrl,List<NameValuePair> urlFields, List<NameValuePair> formData,List<NameValuePair> optionalData ){
      
      // Append params to the URL 
      if (urlFields != null)
          baseUrl = baseUrl + getUrlPathForGet(urlFields);
      
      // adds Optional fields to the Url
      if (optional != null)
          baseUrl = baseUrl + "?" + URLEncodedUtils.format(optionalData, "utf-8");
      
      postData(baseUrl,formData);
      
      }
      
    2. It will append the url params to the base url

      private String getUrlPathForGet(List<NameValuePair> urlFields) {
      
      String path = "";
      
      if (urlFields != null) {
      
          for (NameValuePair pair : urlFields) {
          path = path + "/" + pair.getValue();
          }
      }
      
      return path;
      }
      
    3. Add the form data as entity to HttpPost object with the modified url.

      public void postData(String baseUrl,List<NameValuePair> formData) {
      
      // Create a new HttpClient and Post Header
      HttpClient httpclient = new DefaultHttpClient();
      // pass the url as parameter and create HttpPost object.
      HttpPost post = new HttpPost(baseUrl);
      
      // Add header information for your request - no need to create 
      // BasicNameValuePair() and Arraylist.
      post.setHeader("Authorization", "Bearer " + token);
      post.setHeader("Content-Type", "application/json");
      post.setHeader("Cache-Control", "no-cache");    
      
      try {       
      
      // pass the content as follows:
      post.setEntity(new UrlEncodedFormEntity(formData,
                          HTTP.UTF_8));
      
      // Execute HTTP Post Request
      HttpResponse response = httpclient.execute(post);
      
      // TODO: Process your response as you would like.
      
      } catch (IOException e) {
          // TODO Auto-generated catch block
      }
      }