I am struggling with some issue related with http, java and the stackexchange API
consider the following url as string:
private static final String URLSTRING_2 = "http://freegeoip.net/json/";
if I write this url in my browser I get this answer as json:
now im trying to do that with java and only native libs, for that am using the snippet below wich is working so far so good...
If I parse the json and i try to get the value for the key "country_name" then the snippet prints as spected "Singapore"
public static void main(String[] args) throws Exception {
// Connect to the URL using java's native library
final URL url = new URL(URLSTRING_2);
final HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();
// Convert to a JSON object to print data
final JsonParser jp = new JsonParser(); // from gson
final JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); // Convert the input stream to a json
// element
final JsonObject rootobj = root.getAsJsonObject(); // May be an array, may be an object.
final String country = rootobj.get("country_name").getAsString(); // just grab the zipcode
System.out.println("country_name: " + country);
}
Now my question:
if I do the same with this link
https://api.stackexchange.com//2.2/users/22656?order=desc&sort=reputation&site=stackoverflow&filter=!T6oHjO_RIWkfWpoL5g
my browser outputs the following json:
but if I try to parse the json I get an exception because am getting from the request this:
ý•#‡ž¼ÚRìØ1ôX`»v?±h[‹-¹’/+ò........
for something that is not even human readable...
do you know why?
Thanks in advance
The StackOverflow API GZIP-compresses its response. The reason you see that string of non-human-readable characters is that you are trying to read GZIP-compressed data without first decompressing it.
Your browser is capable of reading this header and doing the decompression itself. Your code isn't yet.
You can confirm that GZIP compression is used by displaying the value of the Content-Encoding header in the response. Adding the line
System.out.println(request.getContentEncoding());
will print out
gzip
Fortunately, fixing the problem is fairly straightforward. You need to wrap the InputStream you get from the request in a GZIPInputStream:
final JsonElement root = jp.parse(new InputStreamReader(new GZIPInputStream((InputStream) request.getContent()))); // Convert the input stream to a json
However, instead of the built-in Java classes, I'd recommend using a library such as Apache HTTPComponents Client for making your HTTP requests. In particular, a library such as this will automatically detect the content-encoding and do the decompression for you.
Related
I'm using IntelliJ to learn RestAssured; this is completely new territory for me. I have a simple .json file in place and I want to have a API Response to assert if it's the same as the mentioned .json file.
Basically: If the output of the call equals what I have in the json file, it's all good.
I used the demo restapi.demoqa.com for quick reference. This is what I have right now:
#Test
public void ComparewithJSONinResources()
{
String CityResponse = ?????
RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.request(Method.GET, "/Hyderabad");
String responseBody = response.getBody().asString();
System.out.println(responseBody);
Assert.assertTrue(responseBody.equals(CityResponse));
response.body();
}
I have the .json file in place called CityResponse.json. For easy reference, say on the location c:/CityResponse.
Is it possible to convert the Json file to a string to assert that the API and the JSON are equal?
Comparing JSON as String will never give accurate results, as you will possibly see inconsistency in space, tabs (indentation), property (key-value pair) sequencing etc. Your best bet is to parse JSON into POJO using one of the many popular libraries (Ex. Jackson, GSON etc). And this deserialization you need for both RestAssured Http response & one you are reading from .json file, and once you have two java objects, use standard Java comparision by overriding equals method.
I'm in the process of converting my website to an Android app and one of the pages' data currently is populated via JSON in my website. The way it works is that the URL generates a different JSON data with the same structure based on the passed ID. I already have the logic for passing the ID to the URL. Now I want to read the data through Java code and parse the JSON children and its values in it.
I have a URL that leads to the JSON file in textual form, but I'm not sure how to go about reading the data from it and accessing the child nodes based on the JSON key.
So I guess what I'm asking is what is the usual approach for this procedure? I see a lot of different examples, but none of which are applicable to my problem.
Anyone have any suggestions as to how I should approach this?
JSONObject = new JSONObject(yourjsonstring);
Now you have your Json Object...
If your Json start with array use this:
JSONArray = new JSONArray(yourjsonarray);
You can use existing libraries to parse JSON, gson or Moshi are two solutions.
The way you go about parsing the JSON is as followed
First you need to make pojo's with the same structure as the JSON file.
then you can parse it to java code via the fromJSON() method, this will make new objects and fill it with the data from the JSON.
gson example for clarification:
Gson gson = new Gson();
Response response = gson.fromJson(jsonLine, Response.class);
where jsonLine = your json file and the Response.Class the pojo in which you want to json to load.
Now you have the JSON values as Java classes in response.
If you're using Retrofit and OkHTTP to perform the network calls i suggest you use Moshi as it's also from Square and claimed to work faster and better than gson. (if you want to know why you can leave a comment).
I think what you're trying to do is this
on post execute method do the following
#Override
protected void onPostExecute(String result) {
String status = "";
String message = "";
String tag = "";
String mail = "";
try {
JSONObject jsonResult = new JSONObject(result);
status = jsonResult.optString("status");
message = jsonResult.optString("message");
tag = jsonResult.optString("tag");
mail = jsonResult.optString("mail");
} catch (JSONException e) {
e.printStackTrace();
}
of course your json array contains different keys
Just reolace them with yours
I am currently parsing a web service that give me Json Documents as response. I want to store those in CouchDb using java, but I cannot find a way. With the couchdb library for java (Ektorp, couchdb4j etc...) I can only store java documents in the database, which would mean I have to transform my raw json to java Document in order to store them.
Do you have any idea how i could directly store raw json ?
Many thanks in advance
The Ektop documentation provides an example of updating a document using JSON stored in a file:
File file = someMethodToGetFile();
InputStream jsonInputStream = new FileInputStream(file);
db.update("document_id",
jsonInputStream,
file.length(),
null);
Instead of using the FileInputStream, if your JSON is in memory in your java program (e.g. as a String), you could wrap the string in a ByteArrayInputSteam:
String yourJsonString = ...
InputStream jsonInputStream = new ByteArrayInputStream(yourJsonString.getBytes());
db.update("document_id",
jsonInputStream,
file.length(),
null);
Alternatively, CouchDB is accessed through a restful API so you can interact with CouchDB using any java library that understands REST, for example using Apache HttpClient:
String yourJsonString = ...
StringRequestEntity requestEntity = new StringRequestEntity(
yourJsonString,
"application/json",
"UTF-8");
PostMethod postMethod = new PostMethod("http://couchdb.server/database");
postMethod.setRequestEntity(requestEntity);
int statusCode = httpClient.executeMethod(postMethod);
I am developing a web-app using AJAX requests on the client-side and Servlets on the server-side.
My aim is to send objects of Javascript to server, then do some manipulations there and send it back to show here.
Let's say my js object is
var obj={hero:"Spiderman",name:"Peter Parker"};
My Approach
1.Convert obj to JSON string and send
var str= JSON.stringify(obj);
xmlhttp.open("POST",myurl,true);
xmlhttp.setRequestHeader("Content-Type","application/json",true);
xmlhttp.send("data="+str);
2. Recieve string,convert this back to JSON, manipulate "name" to "Bruce Wayne" and send it back as string
3.Recieve and convert back to Json
var data= JSON.parse(xmlhttp.responseText);
I am struggling at second point.I am using org.json for it .I searched and read docs but could not find satisfied answer for converting string to json and vica-versa in JAVA in my context.
It would be really helpful one could provide simple working code or point to some links where I can study.
P.S :
I cannot use Jquery as I am using AngularJS. See Why?
I will always send valid JSON string.
I can use other JSON lib. if its good than org.json and satisfy my needs.
Please provide its jar download link.
Assuming you are able to pull out data in your server code
This is how you can do it using org.json:
JSONParser parser = new JSONParser();
JSONObject requestObj = (JSONObject) parser.parse(data);
String name = (string)requestObj.get("name");
name = "Bruce Wayne";
Code to create the response can look something like this:
JSONObject response = new JSONObject();
response.put("name",name);
return response.toJSONString();
This assumes your server method returns a String type
And in case if you are using Servlet you can use HttpServletResponse object res to create response like:
res.setContentType("application/json");
OutputStream os = res.getOutputStream();
os.write(response.toString().getBytes());
os.close();
In my application I retrieve search results in JSON format from an external tool called Google Search Appliance(GSA).
The JSON result from GSA is very large and therefore I prefer to modify the GSA JSON result into something more suitable for displaying on my webpage.
If I directly display the GSA JSON result without formatting it in my java code I'm not facing any encoding issues on my webpage.
But if I format the large GSA JSON result into a suitable JSON format in my servlet java code I'm facing encoding problems.
Example - “All Access Pass” gets displayed as ÂAll Access PassÂ.
I return the modified json from my servlet to the webpage use the following code -
response.setContentType("application/json;charset=UTF-8");
I have tried to change the charset to iso-8859-1 but it does not make any difference.
I edit my original JSON in the following manner -
String responseText = getMethod.getResponseBodyAsString();
JSONObject resultJSON = new JSONObject();
try {
JSONObject jsonObj = new JSONObject(responseText);
JSONArray resultJsonArray = jsonObj
.getJSONArray("RES");
JSONObject searchResultJSON = null;
for (int iCnt = 0; iCnt < resultJsonArray.length(); iCnt++) {
searchResultJSON = new JSONObject();
JSONObject obj = resultJsonArray.getJSONObject(iCnt);
JSONObject metaTagObj = obj
.getJSONObject("MT");
if (metaTagObj.has(("title"))) {
searchResultJSON.put("title",metaTagObj.get("title").toString());
}
resultJSON.accumulate("RES", searchResultJSON);
}
response.setContentType("application/json;charset=UTF-8");
response.getWriter().print(resultJSON);
}catch(JSONException e){}
The modification to the original JSON which I'm going here can be done in JavaScript which would solve my problem but it is something which I do not want to do.
Is there a way to find out the encoding format of the text in the original GSA JSON?
How can I avoid the java code from changing the text encoding in the original GSA JSON?
Please help me understand what is going on here and how I can avoid this problem.
The text encoding problem was happening because the call which is made to the GSA server using Apache HTTP Client was using a default content encoding character set of iso-8859-1 but the GSA server expected the HTTP Client request and response to be in UTF-8 encoding.
This problem got resolved after setting the encoding for HTTPClient -
HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset("UTF-8");
And the servlet response encoding to
response.setContentType("application/json;charset=UTF-8");