1

In my app I am sending a String to the Servlet through BasicNameValuePairs, this way:

        HttpClient httpClient = new DefaultHttpClient(); //127.0.0.1 - 10.201.19.153
        HttpPost httpPost = new HttpPost(conn.urls.get("now"));

        List<NameValuePair> nameValuePairs = new ArrayList<>(1);
        nameValuePairs.add(new BasicNameValuePair("order", order));//"tours"
        if(order.equals("reservation")){
            String booking = new Gson().toJson(reservation);
            nameValuePairs.add(new BasicNameValuePair("reservation", booking));
        }

        try {
            // Add name data to request
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // Execute HTTP Post Request
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            //...
        } //...

is there another way to send a String apart from using BasicNameValuePairs or this is the only way?

Cris
  • 2,002
  • 4
  • 30
  • 51
  • 1
    You can try `MultiPartEntityBuilder`. Though you will need some external jars for it. Here's an example: http://stackoverflow.com/a/22803149/4350275 – Prerak Sola Jul 02 '15 at 15:11

1 Answers1

1

I don't exactly know why u need an alternative but here it is .. instead of using Gson u can use following code

{
...
    List<NameValuePair> params = new ArrayList<>();
     params.add(new BasicNameValuePair("string",longString));
     makeHttpRequest(url,"POST", params);
...
}

    public void makeHttpRequest(String url, String method, List<NameValuePair> params) {


        try {
            if (method == "POST"){
                DefaultHttpClient httpClient= new DefaultHttpClient();
                HttpPost httpPost =new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse=httpClient.execute(httpPost);
                HttpEntity httpEntity=httpResponse.getEntity();
                is=httpEntity.getContent();

            }else if (method == "GET"){

                DefaultHttpClient httpClient=new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params,"utf-8");
                if (!paramString.matches(""))
                {
                url +="?"+paramString;
                }
                HttpGet httpGet = new HttpGet(url);
                lru =url;

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity=httpResponse.getEntity();
                is=httpEntity.getContent();

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

I hope it helps

Mayur
  • 392
  • 1
  • 3
  • 12
  • I need an alternative because when I use that code, the accented characters such as `à` `è` etc. don't get parsed correctly...and therefore I get a `MalformedJsonException` does your line of code: `String paramString = URLEncodedUtils.format(params,"utf-8");` solve this issue? – Cris Jul 02 '15 at 15:47
  • Moreover I need to use a `Post` request – Cris Jul 02 '15 at 15:51
  • I still don't know why this is a problem. utf-8 encoding format just converts such special characters in unique codes for example à is converted to %C3%A0 where % represents 'space' so my code should work correctly and forms a link as expected – Mayur Jul 02 '15 at 17:06
  • yes your code is all right, but I need to use a `post` request, so I've adapted it – Cris Jul 02 '15 at 17:07
  • And to use post method u can use followig code DefaultHttpClien `httpClient= new DefaultHttpClient(); String paramString = URLEncodedUtils.format(params,"utf-8"); if (!paramString.matches("")) { url +="?"+paramString; } HttpPost httpPost =new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse=httpClient.execute(httpPost); HttpEntity httpEntity=httpResponse.getEntity(); is=httpEntity.getContent();` – Mayur Jul 02 '15 at 17:08
  • could you modify your answer according to a `post` request? – Cris Jul 02 '15 at 17:10