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?

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

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?
Moreover I need to use a Post request
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
yes your code is all right, but I need to use a post request, so I've adapted it
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();
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.