How to add json to GET request query parameter? - java

I want json to be sent to GET request in query parameter to get the response for that json request.
If I use a link something like this :
www.url.com/search?query1=abc&filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]}
Then the url part appears blue if I do it as sysout statement, something like this:
www.url.com/search?query1=abc&filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]}
and the json appears as if it is not the part of the request.
To create a URL, I'm appending the manipulated JSON string to the URL and then sending the request. But it appears as two different strings.
Also I have used encoder to encode the JSON part
filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]}
In that case the brackets and double quotes everything in that json is encoded, even the equalTo sign. Also the link appears blue but while sending request it throws exception of 400 Bad Request, since the equalTo is also converted to its encoding format.
I tried encoding only the JSON part leaving the filter= in the url itself, something like this :
www.url.com/search?query1=abc&filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]}
The results that appear after the request is send is different from the results I want.
I'm using following code to create a JSON:
private String getVinFromInventoryRequest(String vin) throws JSONException {
JSONObject request = new JSONObject();
JSONArray orArray = new JSONArray();
for(String vin : vins) {
JSONObject termsObject = new JSONObject();
JSONObject vinsObject = new JSONObject();
JSONArray vinsArray = new JSONArray();
vinsArray.put(vin);
vinsObject.put("vin", vinsArray);
termsObject.put("terms", vinsObject);
orArray.put(termsObject);
}
request.put("or", orArray);
System.out.println("OfferMapper.getVinFromInventoryRequest " + request.toString());
return request.toString();
}

Also look what I found with a little googling :
JSONObject json = new JSONObject();
json.put("someKey", "someValue");
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpPost request = new HttpPost("http://yoururl");
StringEntity params = new StringEntity(json.toString());
request.addHeader("content-type", "application/json");
request.setEntity(params);
httpClient.execute(request);
// handle response here...
} catch (Exception ex) {
// handle exception here
} finally {
httpClient.close();
}
For more info see : HTTP POST using JSON in Java

Related

HTTP Error Code 415 - JSON arrays in JAVA

I am trying to produce and send via JAVA a JSON file and when I am trying to add a nested object with an array in order to fit an application's protocol (which is not important to the question) the java program cannot send the file because of an HTTP error, code 415 (unsupported media type), which is strange because the produced JSON works when I copy it in the destined application (Google's DialogFlow). In other words, JSON is functional but JAVA (version 1.8) does not recognize it. Does anyone have any ideas why that happens?
When the part with the JSONArray in not included in the JSON file the request is sent without problem (see code below). I have tried changing the content-type from "application/json;charset=utf8" to "application/json;charset=utf-8" or "application/json" but nothing worked (this part in not included in the code because the changes that resulted in JSON not working were in the block below).
Part not working:
static JSONObject messageToJSON()
{
JSONObject requestJson = new JSONObject();
JSONObject meta= new JSONObject();
JSONObject payload= new JSONObject();
JSONArray arrayJson = new JSONArray();
String messageData="My Message";
try
{
requestJson.put("message", messageData);
requestJson.put("messageType", "html");
payload.put("title", "Thanks");
payload.put("message", "Thank you");
arrayJson.put(payload);
meta.put("contentType", "300");
meta.put("templateId", "6");
meta.put("contentType", "300");
meta.put("payload", arrayJson);
requestJson.put("metadata", meta);
System.out.println(requestJson.toString());
}
catch (JSONException e)
{
e.printStackTrace();
}
return requestJson;
}
The part working (without the extra layer in JSON, e.g. the payload JSON object and the arrayJson JSON array):
static JSONObject messageToJSON()
{
JSONObject requestJson = new JSONObject();
JSONObject meta= new JSONObject();
JSONObject payload= new JSONObject();
JSONArray arrayJson = new JSONArray();
String messageData="My Message";
try
{
requestJson.put("message", messageData);
requestJson.put("messageType", "html");
meta.put("contentType", "300");
meta.put("templateId", "6");
meta.put("contentType", "300");
requestJson.put("metadata", meta);
System.out.println(requestJson.toString());
}
catch (JSONException e)
{
e.printStackTrace();
}
return requestJson;
}
Ok, I found the problem. I should have transformed the arrayJson from JSONArray type to string.
In other words, instead of
meta.put("payload", arrayJson);
I should have
meta.put("payload", arrayJson.toString());
or make the arrayJson in a string format from the beginning.

Parsing a HttpResponse JSON in Java

I am trying to do a HTTP post request to a web API and then parse the received HttpResponse and access the key value pairs in the body. My code is like this:
public class access {
// http://localhost:8080/RESTfulExample/json/product/post
public static void main(String[] args) {
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("https://XXXXXXX/RSAM_API/api/Logon");
// Request parameters and other properties.
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(2);
urlParameters.add(new BasicNameValuePair("UserId", "XXXXX"));
urlParameters.add(new BasicNameValuePair("Password", "XXXXXX"));
try {
httppost.setEntity(new UrlEncodedFormEntity(urlParameters));
//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line;
while(null !=(line=rd.readLine())){
System.out.println(line);
}
System.out.println(response);
String resp = EntityUtils.toString(response.getEntity());
JSONObject obj = new JSONObject(resp);
}
catch (Exception e){
e.printStackTrace();
}
}
}
I am trying to access the body by converting it to a JSONObject with these 2 lines of code:
String resp = EntityUtils.toString(response.getEntity());
JSONObject obj = new JSONObject(resp);
But I get an error in the second line saying:
JSONObject
(java.util.Map)
in JSONObject cannot be applied
to
(java.lang.String)
Not sure if this is the correct approach. Is there a way to do what I am trying to do?
Any help would be appreciated, Thank you.
EDIT:
So when I try to print the response body using the following lines,
String resp = EntityUtils.toString(response.getEntity());
System.out.println(resp);
I get the result: {"APIKey":"xxxxxxxxxxxxxx","StatusCode":0,"StatusMessage":"Y‌​ou have been successfully logged in."}
I am looking for a way to parse this result and then access each element. Is there a way to do this?
According to JsonSimple's JsonObject documentation it takes map in the constructor but not a String. So the error you are getting what it says.
You should use JSONParser to parse the string first.
Its also better to provide the encoding as part of EntityUtils.toString say UTF-8 or 16 based off your scenario.
IOUtils.toString() from Apache Commons IO would be a better choice to use too.
Try the below line to parse the JSON:
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(resp);
The above lines will vaildate the JSON and through exception if the JSON is invalid.
You don't need to read the response in the extra while loop. EntityUtils.toString(response.getEntity()); will do this for you. As you read the response stream before, the stream is already closed when comming to response.getEntity().

Extract string from http response in java client

I want to extract the string returned from java web service in java client. The string returned from java web service is as follows:
{"Name":"Raj Johri","Email":"mailraj#server.com","status":true}
Which is a Json string format. I have written client code to extract this string as follows:
public static void main(String[] args) throws Exception{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost:8080/JsonWebService/services/JsonWebService/getData");
post.setHeader("Content-Type", "application/xml");
HttpResponse httpres = httpClient.execute(post);
HttpEntity entity = httpres.getEntity();
String json = EntityUtils.toString(entity).toString();
System.out.println("json:" + json);
}
I am getting following print on the console for json as:
json:<ns:getDataResponse xmlns:ns="http://ws.jsonweb.com"><ns:return>{"Name":"Raj Johri","Email":"mailraj#server.com","status":true}</ns:return></ns:getDataResponse>
Please tell me how to extract the string
{"Name":"Raj Johri","Email":"mailraj#server.com","status":true}
which is the actual message. Thanks in advance...
Well, The respons is as type of xml, and your json is in the <ns:return> node , so i suggest you to enter in depth of the xml result and simply get your json from the <ns:return> node.
Note:
I suggest you to try to specifying that you need the response as JSON type:
post.setHeader("Content-type", "application/json");
post.setHeader("Accept", "application/json");
There is a dirty way to do this (beside the xml parsing way)
if you are getting the same XML every time,
you can use split()
String parts[] = json.split("<ns:return>");
parts = parts[1].split("</ns:return>");
String jsonPart = parts[0];
now jsonPart should contain only {"Name":"Raj Johri","Email":"mailraj#server.com","status":true}

Creating JSON in android

Please have a look at the following. They are JSON formats. Don't worry about the username and id. THhey are fake. You can see the link here
Authenticate
POST /andromeda/prediction?username=alfred;api_key=79138a622755a2383660347f895444b1eb927730; HTTP/1.1
Host: bigml.io
Content-Type: application/json
Creating Prediction
curl https://bigml.io/andromeda/prediction?$BIGML_AUTH \
-X POST \
-H 'content-type: application/json' \
-d '{"model": "model/50a2eac63c19200bd1000008", "input_data": {"000001": 3}}'
Above, BIGML_AUTH means a variable which includes the user name and the API key.
Now, I am going to do these JSON calls with android. Below is my code
private JSONObject putJson()
{
HttpClient httpclient = new DefaultHttpClient();
JSONObject finalResult = null;
HttpPost httppost = new HttpPost("https://bigml.io/andromeda/prediction?username=alfred;api_key=79138a622755a2383660347f895444b1eb927730");
httppost.setHeader("Content-type", "application/json; charset=utf-8");
JSONObject json = new JSONObject();
try {
// Add your data
json.put("model","model/50a2eac63c19200bd1000008");
json.put("input_data", "{000001:3}");
StringEntity se = new StringEntity( json.toString());
httppost.setEntity(se);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String jsonString = reader.readLine();
JSONTokener tokener = new JSONTokener(jsonString);
finalResult = new JSONObject(tokener);
}
catch(Exception e)
{
Log.d("Error here", "Error is here",e);
}
return finalResult;
}
I need to know whether I have coded the given Json in Android correctly. I can't run and check because when run and checked, it returns ID DOES NOT EXIST error.
Please let me know whether I have coded the JSON correctly, because I have not use JSON before.
There is a slight difference between your code and curl:
Your code:
// Add your data
json.put("model","model/50a2eac63c19200bd1000008");
json.put("input_data", "{000001:3}");
If you want to produce equivalent JSON as in your curl command, it should read:
// Add your data
json.put("model","model/50a2eac63c19200bd1000008");
JSONObject input_data = new JSONObject();
input_data.put("000001", 3);
json.put("input_data", input_data);
I encountered another thing which could cause problems. You read only the first line of your response. In many cases this is not a problem, because most JSON Objects are in single line. But if somebody enables DEBUG-mode on the API-server it can happen that the JSON object comes in multiple lines (aka. human readable).
BTW Google doesn't encourage developers to use DefaultHttpClient for Android versions > FROYO (makes ~ 97% of phones). For GINGERBREAD and newer we should use HttpUrlConnection for requesting web resources and REST/JSON.
I've made a small library called DavidWebb which makes live easier when making HTTP requests. With this library your code would look like this:
private JSONObject putJson() throws Exception {
JSONObject result = null;
try {
JSONObject json = new JSONObject();
// Add your data
json.put("model", "model/50a2eac63c19200bd1000008");
JSONObject input_data = new JSONObject();
input_data.put("000001", 3);
json.put("input_data", input_data);
Webb webb = Webb.create();
result = webb
.post("https://bigml.io/andromeda/prediction?username=alfred;api_key=79138a622755a2383660347f895444b1eb927730")
.body(json)
.ensureSuccess()
.asJsonObject()
.getBody();
} catch (JSONException e) {
// should not happen
// Log.d("Error here", "Error is here",e);
} catch (WebbException e) {
// carries the real exception
}
return result;
}
The link I've provided gives many alternatives for HTTP-request libraries for Android - you don't have to use DavidWebb.
org.json.JSONObject?
If so, then here your json code is fine & will give valid json too!
{
"model": "model/50a2eac63c19200bd1000008",
"input_data": "{000001:3}"
}

Bing Maps REST services returns incorrect characters for French. Is there a way to request them to encode their response or decode it somehow?

In the Android application I have written, there is a portion which allows the user to enter the start and end location of their trip, and a route itinerary is returned. I am using Bing Maps REST services for this. I want the directions returned to be in French.
A sample request: request. This is best seen on a Chrome browser, Safari and Firefox take care of this. You can see that the directions have lots of strange characters where they are not supposed to be. I have tried decoding on the device, by doing:
URLDecoder.decode(obj.optString("text"), HTTP.ISO_8859_1)
which does not work (the response stays the same), which makes sense I think since it has already become the special characters. I cannot use Windows-1252 to decode because Android does not seem to support that.
An example of what I am being sent back: Léger Encombrement. What it should be: Léger Encombrement.
It works perfectly on an iPhone as well, but not on Android. Any suggestions on how I can solve this?
My code in the connection class is:
public static JSONObject getJSONResult(final String url) {
try {
final HttpClient client = new DefaultHttpClient();
final HttpGet get = new HttpGet(url);
final HttpResponse responsePost = client.execute(get);
final HttpEntity resEntity = responsePost.getEntity();
boolean DEBUG = true;
if (DEBUG) {
Log.d("", "[JSON-ENV] url: " + url);
}
final String str = EntityUtils.toString(resEntity);
Log.d("connection", "response str: " + str);
if (resEntity != null) {
final JSONObject obj = new JSONObject(str);
Log.d("connection", "JSON RESPONSE IS " + obj);
return obj;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Is there something I need to add into my connection class?
UPDATE:
I added the JSON parsing code to format as "ISO_8859_1" as seen at this link: http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/ but I still get the same results ...
This is JSON. You don't need to use URLDecoder. The error is before that, probably when you create the String for the JSON Parser. JSON is always in UTF-8 (or 16, rarely)
Can you post the code for reading the server response?
edit
EntityUtils uses ISO_8859_1 as a default Charset if it does not find one in the content. Simply change
final String str = EntityUtils.toString(resEntity);
to
final String str = EntityUtils.toString(resEntity, HTTP.UTF_8);

Categories