Dzmitry Kalachou
Dzmitry Kalachou

Reputation: 11

Send key-value parametres and json message body throught http post request

I need to send http POST request from mobile android application to the server side applcation. This request need to contain json message in body and some key-value parametres. I am try to write this method:

 public static String makePostRequest(String url, String body,  BasicHttpParams params) throws ClientProtocolException, IOException {
        Logger.i(HttpClientAndroid.class, "Make post request");
        HttpPost httpPost = new HttpPost(url);
        StringEntity entity = new StringEntity(body);
        httpPost.setParams(params);
        httpPost.setEntity(entity);
        HttpResponse response = getHttpClient().execute(httpPost);
        return handleResponse(response);
    }

Here i set parametres to request throught method setParams and set json body throught setEntity. But it isn't work. Can anybody help to me?

Upvotes: 0

Views: 2494

Answers (1)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

You can use a NameValuePair to do this..........

Below is the code from my project where I used NameValuePair to sent the xml data and receive the xml response, this will provide u some idea about how to use it with JSON.

public String postData(String url, String xmlQuery) {



    final String urlStr = url;
    final String xmlStr = xmlQuery;
    final StringBuilder sb  = new StringBuilder();


    Thread t1 = new Thread(new Runnable() {

        public void run() {

            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost(urlStr);


            try {

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        1);
                nameValuePairs.add(new BasicNameValuePair("xml", xmlStr));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpclient.execute(httppost);

                Log.d("Vivek", response.toString());

                HttpEntity entity = response.getEntity();
                InputStream i = entity.getContent();

                Log.d("Vivek", i.toString());
                InputStreamReader isr = new InputStreamReader(i);

                BufferedReader br = new BufferedReader(isr);

                String s = null;


                while ((s = br.readLine()) != null) {

                    Log.d("YumZing", s);
                    sb.append(s);
                }


                Log.d("Check Now",sb+"");




            } catch (ClientProtocolException e) {

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

    });

    t1.start();
    try {
        t1.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    System.out.println("Getting from Post Data Method "+sb.toString());

    return sb.toString();
}

Upvotes: 1

Related Questions