Java Http request on api.stackexchange with json response - java

I'm trying to use the api-stackexchange with java but when I do the request and try to parse the response with a json parser I have an error.
public ArrayList<Question> readJsonStream(InputStream in) throws IOException {
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
reader.setLenient(true);
try {
System.out.println(reader.nextString()); // � special character
return readItem(reader);
} finally {
reader.close();
}
}
public ArrayList<Question> readItem(JsonReader reader) throws IOException {
ArrayList<Question> questions = new ArrayList<Question>();
reader.beginObject();
while (reader.hasNext()) {
System.out.println("here");//not print the error is before
String name = reader.nextName();
if (name.equals("items")) {
questions = readQuestionsArray(reader);
}
}
reader.endObject();
return questions;
}
public final static void main(String[] args) throws Exception {
URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("api.stackexchange.com").setPath("/2.0/search")
.setParameter("site", "stackoverflow")
.setParameter("intitle" ,"workaround")
.setParameter("tagged","javascript");
URI uri = builder.build();
String surl = fixEncoding(uri.toString()+"&filter=!)QWRa9I-CAn0PqgUwq7)DVTM");
System.out.println(surl);
Test t = new Test();
try {
URL url = new URL(surl);
t.readJsonStream(url.openStream());
} catch (IOException e) {
e.printStackTrace();
}
}
And the error is:
com.google.gson.stream.MalformedJsonException: Expected literal value
at line 1 column 19
Here is an example of the Json :
{
"items": [
{
"question_id": 10842231,
"score": 0,
"title": "How to push oath token to LocalStorage or LocalSession and listen to the Storage Event? (SoundCloud Php/JS bug workaround)",
"tags": [
"javascript",
"javascript-events",
"local-storage",
"soundcloud"
],
"answers": [
{
"question_id": 10842231,
"answer_id": 10857488,
"score": 0,
"is_accepted": false
}
],
"link": "http://stackoverflow.com/questions/10842231/how-to-push-oath-token-to-localstorage-or-localsession-and-listen-to-the-storage",
"is_answered": false
},...
Here is the URL of the request:
https://api.stackexchange.com/2.0/search?tagged=javascript&intitle=workaround&site=stackoverflow&filter=!)QWRa9I-CAn0PqgUwq7)DVTM
So what's the problem? Is the Json really malformed? Or did I do something not right?
Thanks, Anthony
Edit:
I'm now sure that the problem come to the request, I paste the response of the request via a browser in a text file that I host in a server Apache and it works fine. I am abble to parse the Json of the response.

Change this code:
if (name.equals("items")) {
questions = readQuestionsArray(reader);
}
to this code:
if (name.equals("items")) {
questions = readQuestionsArray(reader);
} else {
reader.skipValue();
}
Otherwise you end up calling nextName() twice in a row, which is invalid.

The data in the response is compressed with the deflate algorithm. So, I encapsulated the InputStream with a GZIPInputStream:
public final static void main(String[] args) throws Exception {
URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("api.stackexchange.com").
setPath("/2.0/search").
setParameter("site", "stackoverflow").
setParameter("intitle" ,"workaround").
setParameter("tagged","javascript");
URI uri = builder.build();
ArrayList<Question> q =null;
String result = "";
String surl = fixEncoding(uri.toString()+"&filter=!)QWRa9I-CAn0PqgUwq7)DVTM");
System.out.println(surl);
Test t = new Test();
try {
URL url = new URL(surl);
q = t.readJsonStream(new GZIPInputStream(url.openStream()));
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println(result);
for (Question question : q) {
System.out.println(question.title);
}
}

Related

How to get a Json object and store it in a Hashmap

In my dopost, I'm trying to get a Json object from the request and then create a Profile object from that request using fromJson and then store it in my hashmap. When I try and do that in my code and print out my hashmap, that new profile i just created just prints out {id=0} but all other profiles that I hard coded in print out fine. My new profile doesn't print out my username lastname age etc.
Does anyone know what I'm doing wrong here?
What I'm trying to sent as json
{
"4": {
"id": "9",
"username": "gfgf",
"lastname": "hgfh",
"favTeam": "Manc city",
"age": "51"
}
}
my code
public class ProfileServlet extends HttpServlet
{
protected HashMap<Integer, Profile> team = new HashMap<Integer, Profile>();
private static final long serialVersionUID = 1L;
private Gson gson = new Gson();
public ProfileServlet()
{
Profile profile1 = new Profile(1,"bob","bee","Manc city","21");
Profile profile2 = new Profile(2,"billy","smith","Dortmud","25");
Profile profile3 = new Profile(3,"john","jamesd","Aston Villa","44");
int id = 1;
int id2 = 2;
int id3 = 3;
team.put(id,profile1);
team.put(id2,profile2);
team.put(id3,profile3);
}
public void sendAsJson(HttpServletResponse response, Object obj) throws IOException
{
PrintWriter out = response.getWriter();
response.setContentType("application/json; charset=UTF-8");
String jsonString = gson.toJson(obj);
out.print(jsonString);
out.flush();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("application/json; charset=UTF-8");
int counter = 0;
boolean breakloop = false;
PrintWriter out = response.getWriter();
String path = request.getPathInfo();
if(path == null || path.equals("/"))
{
StringBuilder buffer = new StringBuilder();
BufferedReader reader = request.getReader();
String line;
while ((line = reader.readLine()) != null)
{
buffer.append(line);
}
String myprofile = buffer.toString();
Profile newp = gson.fromJson(myprofile, Profile.class);
//loop to create my IDs in order
while(breakloop == false)
{
if(!team.containsKey(counter))
{
newp.id = counter;
breakloop = true;
}
else
counter++;
}
team.put(newp.id,newp);
sendAsJson(response,team);
}
else
{
response.sendError(400, "Incorrect request, make sure you typed the body correctly!");
}
}
}
I solved my problem, nothing is wrong with my code but the problem was in my json request. This worked for me.
How my Json works, I guess adding those outer brackets somehow didn't get my full request
{
"id": "9",
"username": "gfgf",
"lastname": "hgfh",
"favTeam": "Manc city",
"age": "51"
}

Post Request returning error 400 with Java

I don't really see where is my mistake in my java code.
I have to log in to Kofax Total Agility using REST API. For this I tried to use postman to test if my json was correctly built. Here is my login JSON :
{
"userIdentityWithPassword": {
"LogOnProtocol": "7",
"UnconditionalLogOn": false,
"UserId": "myLogin",
"Password": "myPassword"
}
}
I obtain a positive answer :
{
"d": {
"__type": "Session2:http://www.kofax.com/agility/services/sdk",
"SessionId": "1DE6B79F34054D58AEE1509FE583811F",
"ResourceId": "873C0F5C8BD34BAFBF4B14FF538FBAEC",
"DisplayName": "Aurore Mouret",
"IsValid": true,
"LogonState": {
"__type": "LogonState:http://www.kofax.com/agility/services/sdk",
"FormName": "",
"LogonStateType": 0
},
"ReserveLicenseUsed": false
}
}
So far, so good. For this I created models :
public class UserIdentityWithPasswordRestRequestModel {
LogOnWithPassword2RestRequestModel userIdentityWithPassword;
}
public class LogOnWithPassword2RestRequestModel {
#SerializedName("LogOnProtocol")
private String logOnProtocol;
#SerializedName("UnconditionalLogOn")
private boolean unconditionalLogOn;
#SerializedName("UserId")
private String userId; // C640521793431F4486D4EF1586672385
#SerializedName("Password")
private String password; // 123456
}
For the response :
public class LogOnWithPassword2RestResponseModel {
private DRestResponseModel d;
}
public class DRestResponseModel {
#SerializedName("__type")
private String type;
#SerializedName("SessionId")
private String sessionId;
#SerializedName("ResourceId")
private String resourceId;
#SerializedName("DisplayName")
private String displayName;
#SerializedName("IsValid")
private boolean isValid;
#SerializedName("LogonState")
private LogonStateRestResponseModel logonState;
#SerializedName("ReserveLicenseUsed")
private boolean reserveLicenseUsed;
}
public class LogonStateRestResponseModel {
#SerializedName("__type")
private String type;
#SerializedName("FormName")
private String formName;
#SerializedName("LogonStateType")
private String logonStateType;
}
Those classes should allow me to build the json.
Now I created a method that build the request object and expect a reponse object.
public LogOnWithPassword2RestResponseModel logOnWithPassword() throws Exception {
LogOnWithPassword2RestResponseModel returnValue = new LogOnWithPassword2RestResponseModel();
// set the HTTP Connection to the KTA Application
URL url = new URL("http://localhost/TotalAgility/Services/SDK/UserService.svc/json/LogOnWithPassword2");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setDoOutput(true);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
LogOnWithPassword2RestRequestModel userIdentityWithPassword = new LogOnWithPassword2RestRequestModel();
// set the values
userIdentityWithPassword.setLogOnProtocol(logOnProtocol);
userIdentityWithPassword.setUnconditionalLogOn(unconditionalLogOn);
userIdentityWithPassword.setUserId(userId);
userIdentityWithPassword.setPassword(password);
UserIdentityWithPasswordRestRequestModel userIdentityWithPasswordRestRequestModel =
new UserIdentityWithPasswordRestRequestModel();
userIdentityWithPasswordRestRequestModel.setUserIdentityWithPassword(userIdentityWithPassword);
// Convert to Json :
String jsonInputString = gson.toJson(userIdentityWithPasswordRestRequestModel);
System.out.println(jsonInputString);
// add request parameter, form parameters
try(OutputStream os = con.getOutputStream()) {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
System.out.println("OS " + os);
}
// get the response from KTA
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response.toString());
returnValue = gson.fromJson(response.toString(), LogOnWithPassword2RestResponseModel.class);
System.out.println(returnValue);
}
return returnValue;
}
When i call this part of code, I note that I build the "right" JSON :
{
"userIdentityWithPassword": {
"LogOnProtocol": "7",
"UnconditionalLogOn": false,
"UserId": "myLogin",
"Password": "myPassword"
}
}
For a reason I can't explain, I obtain an error 400.
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: http://94.247.28.163/TotalAgility/Services/SDK/UserService.svc/json/LogOnWithPassword2
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
at com.signature.app.ui.controller.DocumentController.logOnWithPassword(DocumentController.java:71)
at com.signature.app.Main.main(Main.java:21)
The line 71 is corresponding to this line of the try catch
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)))
I replaced
con.setRequestProperty("Content-Type", "application/json; utf-8");
With this code :
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");

org.json.JSONException: JSONObject["status"] is not a JSONObject

I am now currently using a weather API from http://wiki.swarma.net/index.php?title=%E5%BD%A9%E4%BA%91%E5%A4%A9%E6%B0%94API/v2 and wished to convert the JSONObject into printable Strings. However, when I am working on the following code, two errors occurred:
public class getApi {
private static final String WEATHER_MAP_URL = "https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/realtime.json";
private static final String WEATHER_TEST_API = "TAkhjf8d1nlSlspN";
public static JSONObject getWeatherJson() {
try {
URL url = new URL( WEATHER_MAP_URL );
HttpURLConnection connection =
(HttpURLConnection)url.openConnection();
connection.addRequestProperty( "x-api-key", WEATHER_TEST_API );
BufferedReader reader = new BufferedReader(
new InputStreamReader( connection.getInputStream()) );
StringBuffer json = new StringBuffer( 1024 );
String tmp;
while( (tmp = reader.readLine()) != null )
json.append(tmp).append("\n");
reader.close();
JSONObject data = new JSONObject( json.toString() );
if(data.getJSONObject("status").toString() != "ok" ) {
return null;
}
return data;
}
catch(Exception e) {
e.printStackTrace();
return null;
}
}
public static void main( String[] args ) {
JSONObject WeatherJson = getWeatherJson();
try {
JSONArray details = WeatherJson.getJSONObject("result").getJSONObject("hourly").
getJSONArray("skycon");
System.out.println(details.getJSONObject(0).getJSONObject("value").toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The JSONObject structure, which is also shown in the link above, is like this:
{
"status":"ok",
"lang":"zh_CN",
"server_time":1443418212,
"tzshift":28800,
"location":[
25.1552, //latitude
121.6544 //longitude
],
"unit":"metric",
"result":{
"status":"ok",
"hourly":{
"status":"ok",
"skycon":[
{
"value":"Rain",
"datetime":"2015-09-28 13:00"
},
{
...
}]
}
}
}
The error occurred:
org.json.JSONException: JSONObject["status"] is not a JSONObject.
at org.json.JSONObject.getJSONObject(JSONObject.java:557)
at getApi.getWeatherJson(getApi.java:34)
at getApi.main(getApi.java:45)
Exception in thread "main" java.lang.NullPointerException
at getApi.main(getApi.java:47)
I have looked at similar posts on the topic is not a JSONObject Exception but found that none of them can help me. I suspect that something is wrong with requesting the data, so actually, getWeatherJson() returns a null object and results in the NullPointerException and JSONObjectException.
Can anyone help me with the code?
According to the getJSONObject() Javadoc, this method will throw an exception if the returned object isn't a true JSON object, which it isn't because "status" is a string. As such, try using data.getString("status").
The status field in the JSON document you have posted is not an object. In JSON, objects are enclosed in with {} brackets. The result node however, is a nested object which holds the status key/value pair. Try the following:
JSONObject data = new JSONObject(json.toString());
if(data.getJSONObject("result").get("status").toString() != "ok" ) {
return null;
}

Parsing json which has an object in two formats

I have a json object as below.
{
"products": [
{
"details": {
"name": "xxx",
"price": "100rs"
},
"description": "Buy this product"
}, {
"details": [{
"name": "yyy",
"price": "200rs"
}],
"description": "another project"
}
]
}
Here the details are presented in 2 formats. How can I create a POJO (Plain Old Java Object) class of this to use for the Retrofit api?
I think that's bad api response and should be fixed from backend. But if you want to address the problem, you have to deserialize response to String using String converter. You can't do deserialize it to your Pojo using Gson converter.
StringConverter.java
public class StringConverter implements Converter {
#Override
public Object fromBody(TypedInput typedInput, Type type) throws ConversionException {
String text = null;
try {
text = fromStream(typedInput.in());
} catch (IOException ignored) { }
return text;
}
#Override
public TypedOutput toBody(Object o) {
return null;
}
public static String fromStream(InputStream in) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String newLine = System.getProperty("line.separator");
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
out.append(newLine);
}
return out.toString();
}
}
API Call implementation
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.setConverter(new StringConverter())
.build();
YourAPI api = restAdapter.create(YourAPI.class);
api.yourService(parameter,new RestCallback<String>() {
#Override
public void success(String response, Response retrofitResponse) {
super.success(response, retrofitResponse);
//process your response here
//convert it from string to your POJO, JSON Object, or JSONArray manually
}
#Override
public void failure(RetrofitError error) {
super.failure(error);
}
});

Android - Save JSON from InputStream into String

I'm trying to parse this JSON I get from a HttpURLConnection in Android.
{
"responsejson":
{
"value1": [
{
"data": "Call",
"label": "Call",
"default": false
},
{
"data": "Email",
"label": "Email",
"default": false
}
],
"value2": [
{
"attributes": {
"type": "Status",
"url": "/..."
},
"IsOpened": false,
"IsDefault": true,
"TechLabel": "NotStarted",
"Id": "01Jb"
},
{
"attributes": {
"type": "Status",
"url": "/..."
},
"IsOpened": false,
"IsDefault": false,
"TechLabel": "InProgress",
"Id": "01Jb"
},
{
"attributes": {
"type": "Status",
"url": "/..."
},
"IsOpened": true,
"IsDefault": false,
"TechLabel": "Completed",
"Id": "01Jb"
}
],
...
}
}
What I want to do is save the content of value1 in a string, the content of value2 in another string,... because I need to store it in the database, so in the future I can load and parse it. I am using JsonReader but it's not possible to do this with JsonReader.
// ...
inputStream = conn.getInputStream();
JsonReader json = new JsonReader(new InputStreamReader(in));
json.beginObject();
while (json.hasNext()) {
String valueName = json.nextName();
// String content = ?????
}
json.endObject();
// ...
Any ideas? Custom objects are not possible due to we never know which values the JSON is going to show.
Use this to convert JSON array to string
private String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the
* BufferedReader.readLine() method. We iterate until the
* BufferedReader return null which means there's no more data to
* read. Each line will appended to a StringBuilder and returned as
* String.
*/
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
Use Gson to parse the JSON that you receive in InputStream. Then you can get the ArrayList from that parsed object. Again, use Gson to serialize the arraylist back to JSON.
This code works for your example json.
public class Value1 {
public String data,label;
#SerializedName("default")
public boolean isdefault;
}
public class Value2 {
public Attributes attributes;
public boolean IsOpened,IsDefault;
public String TechLabel,Id;
}
public class Attributes {
public String type,url;
}
String jsonString = "{\"responsejson\":{\"value1\":[{\"data\":\"Call\",\"label\":\"Call\",\"default\":false},{\"data\":\"Email\",\"label\":\"Email\",\"default\":false}],\"value2\":[{\"attributes\":{\"type\":\"Status\",\"url\":\"/...\"},\"IsOpened\":false,\"IsDefault\":true,\"TechLabel\":\"NotStarted\",\"Id\":\"01Jb\"},{\"attributes\":{\"type\":\"Status\",\"url\":\"/...\"},\"IsOpened\":false,\"IsDefault\":false,\"TechLabel\":\"InProgress\",\"Id\":\"01Jb\"},{\"attributes\":{\"type\":\"Status\",\"url\":\"/...\"},\"IsOpened\":true,\"IsDefault\":false,\"TechLabel\":\"Completed\",\"Id\":\"01Jb\"}]}}";
try {
org.json.JSONObject object = new JSONObject(jsonString);
jsonString = object.getString("responsejson");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(jsonString).getAsJsonObject();
List<Value1> list1 = new Gson().fromJson(obj.get("value1"), new TypeToken<List<Value1>>() {}.getType());
List<Value2> list2 = new Gson().fromJson(obj.get("value2"), new TypeToken<List<Value2>>() {}.getType());
Since you do not know json structure beforehand, your best bet is to use GSON 2.0 feature that supports default maps and lists.
Use the following code to deserialize :
Object object = new Gson().fromJson(jsonString, Object.class);
The created object is a Map (com.google.gson.internal.LinkedTreeMap) which looks like this (for the above example)
{responsejson={value1=[{data=Call, label=Call, default=false}, {data=Email, label=Email, default=false}], value2=[{attributes={type=Status, url=/...}, IsOpened=false, IsDefault=true, TechLabel=NotStarted, Id=01Jb}, {attributes={type=Status, url=/...}, IsOpened=false, IsDefault=false, TechLabel=InProgress, Id=01Jb}, {attributes={type=Status, url=/...}, IsOpened=true, IsDefault=false, TechLabel=Completed, Id=01Jb}]}}
Use the generated object, parse it and save it in your db.
You can serialize that map back to JSON using :
String json = new Gson().toJson(object);
Hope this helps you.
just read the stream regularly and save it into a regular String, then parse that String :
// to get the general object that contains all the values
JSONObject json = new JSONObject(json_readed);
JSONObject response = json.getJSONObject("responsejson");
// to get the values
List<JSONArray> all_values = new ArrayList<JSONArray>();
Iterator<?> keys = response.keys();
while( keys.hasNext() ){
String value = (String)keys.next();
if( response.get(value) instanceof JSONArray ){
all_values.add(response.getJSONArray(value));
}
}
now you have all the values(whatever what's it's name id) combined into that ArrayList called(all_values).
Note that the JSON you provided in your question is missing opening"{" and closing"}" brackets in the beginning and the ending of it.
What you need to do is, first create a JsonObject from the json string representation, at this stage no specifics are given.
JSONObject object = new JSONObject("json_here"); //catch all exceptions thrown.
Interestingly you mentioned that the structure varies, it consider that weird, i am guessing you are pulling from different api instances. What you need to do , create a pojo class mapping the api instance name to the returned json string body.
After you attained the Object of interest, consider using GSON. A Java serialization/deserialization library to convert Java Objects into JSON and back. What you then need to do is to,serialize the pojo class,into an object.Then store into the database. I recommend using realm and not SQLite.
Example serializing the class.
class JClass {
private String jType;
private String json_body;
JClass() {
// no-args constructor
}
}
JClass j = new JClass();
j.jType ="some_type";
j.json_body = "json_body_here";
Gson gson = new Gson();
String json = gson.toJson(j);
then get the json String object, and store in database of choice.
/*
* Method to parse InputStream to String JSON
* */
private String parse(InputStream in){
StringBuilder result;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
Log.d("JSON Parser", "result: " + result.toString());
return result.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
JSONParser parser = new JSONParser();
List<String> output = new ArrayList<String>();
try {
Object obj = parser.parse(data); // data is JSON
JSONObject jsonObject = (JSONObject) obj;
JSONArray msg = (JSONArray) jsonObject.get("value1");
JSONArray msg2 = (JSONArray) jsonObject.get("value2");
Iterator<String> iterator = msg.iterator();
while (iterator.hasNext()) {
output.add(iterator.next());
}
String[] stringArray = output.toArray(new String[0]);
return stringArray;
} catch (Exception e) {
return null;
}

Categories