chethanks
chethanks

Reputation: 47

retrieve json in servelt sent from android

I am facing an issue while retrieving JSON object sent as an HttpEntity from android in Java Servelt. Below is my code

Android Code:

**

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("order", d));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));    
httpResponse = httpClient.execute(httpPost);

**

Servlet Code:

InputStream in = request.getInputStream();
    StringBuffer xmlStr=new StringBuffer();
    int d;
    while((d=in.read()) != -1){
              xmlStr.append((char)d);
    }
    System.out.println("xmlStr1--"+xmlStr.toString());

The above statement in the servelt code prints the data in the JSON but all garbage value like this

xmlStr1--order=%7B%22orderno%22%3A%223%22%2C%22tableid%22%3A%227%22%2C%22kotid%22%3A%223%22%2C%22stewardid%22%3A%223%22%2C%22items%22%3A%220%22%7D

Please help me retrieve this JSON which is sent as an HttpEntity from the app.

Upvotes: 1

Views: 96

Answers (2)

Bradford Needham
Bradford Needham

Reputation: 681

The data is not in JSON format, or XML format. Your code creates Url-Encoded format. URL Encode and Decode Special character in Java describes code that decodes what you've created, but that code has its own problems.

If you wish to encode the NameValuePair list in JSON format, you can use GSON on Android and GSON or Jackson in the servlet. See https://code.google.com/p/google-gson/ and http://jackson.codehaus.org/

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691685

You should simply use

String order = request.getParameter("order");

Upvotes: 0

Related Questions