Android HTTPPost body parsing - java

I am having a really challenging time parsing some XML data returned to my Android app.
The data is sent as XML but printing it on my mobile screen, it comes out as the following:
{"sessid":"5eed0b52c6953b52e262b559b5557be4","session_name":"SESS6cbf091341a26e4687fa7850b465755a,"user":{"uid":"15","name":"guest","pass":"084e0343a0486ff05530df6c705c8bb4","mail":"adeoduye#hotmail.com", "mode":"0","sort":"0","threshold":"0","theme":"","signature":"","signature_format":"0","created":"1306008217","access":"1306094503","login":"1306134979","status":"1","timezone":"3600","language":"","picture":"","init":"adeoduye#hotmail.com","data":a:1:{s:13:\"form_build_id\";s:37:\"form-49ea7a4ef10a8a2b31478696f17e8dee\";","form_build_id":"form-49ea7a4ef10a8a2b31478696f17e8dee","roles":{"2":"authenticated user","3":"guest"}}}
Can anyone please help a newbie and give me some ideas on how to parse this type of output and/or plain XML?

This isn't XML but JSON. You have to parse that string using the JSON API.
Basically you create a JSONObject by feeding the string into a JSONTokenizer. You can now query the values from the JSONObject as described in the API reference example.

The String you're seeing here is in JSON format. You can parse this in Andriod using the following library : http://code.google.com/p/google-gson/
For more info on json, checkout http://json.org.

Related

How to convert JSON object received in response to REST Client request to a MT940 Swift text file in Java?

How to convert sample JSON given below to a MT940 txt file:
This JSON would be a bad sample to show but hope you get the gist of it...
Just like we have a library in place to parse MT940 strings/txt we also have a library which can help construct a MT940 txt file in Java.
{
"accNumber":"123356",
"openBalInd":"D",
"openBalaDate":"200605",
"curr":"Dollar",
"transactions":[
{
"amount":""434,
"credit/debit":"1000",
"datetime":"20042020"
},
{
"amount":""434,
"credit/debit":"1000",
"datetime":"20042020"}]
}
MT940 is SWIFT message type.
Your input is JSON and the output is MT940 text file.
It is always good to have some java model classes representing your json.
Deserialise json input using Jackson to model you gonna use internally.
Use your own library or some third party like https://www.prowidesoftware.com/resources/SWIFT-writer to convert your internal model to MT940
Serialize the result into text file.

Json Parser not working on request mapping

Problem:
I am unable to parse json request to an object containing double quotes in it.
For example:
jsonString = {
"desc":"Hello stackOverFlow, please reach on this email "asdas#gmail.com". thanks";
}
When I am trying to convert this I am not able parse to an variable, because it looks like invalid json but in real time the request has double quotes in it.
Please show me some good parsing techniques which can do parse this type of requests.
Thanks
You have to escape all of the double quotes, so for example:
String json = "\"{\"desc\":\"Hello stackOverFlow, please reach on this email \"asdas#gmail.com\". thanks\"}";
As you can see it is a lot of work to create very simple JSON, so you are better of using some library for that. I recommend you org.json, it is very lightweight and easy to use. Using it, it would look like this:
JSONObject json = new JSONObject();
json.put("description", "Your description....");
String jsonString = json.toString();

Does anyone know how to change response error format in Spring security oauth2?

I am using Spring security oauth2. By default oauth2 returns it's own error format like {error : "Invalid_grant", error_description : "something"}. I want to change it my own custom format so in my application, it remains consistent. Can anyone please help me? I have gone through lots of links but didn't find any suitable solution till now.
What you get as a result is a JSON document.
Look at Jackson or Gson libraries for example to parse(deserialize) JSON documents. You can get data values 1 by 1 or deserialize into a class instance.
Once you parse modify the data as you wish
Use the same library Jackson or Gson to write(serialize) a new JSON document.
Jackson may also produce as output XML, YAML and CSV documents
https://github.com/FasterXML/jackson-core
https://github.com/google/gson

Reading lots of data returned by API

I am reading from an API provided by a company, but the problem is that one of the accounts from which I am getting the data has around 22000 json objects, it reads fine with small amounts of data, i would say up to 8000 records, but then I get issues like the json is not well formatted besides the problem of being able to read the response.
The response comes this way:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://ywers.com">
[{"Name":"Edward", "LastName":"Jones", "Address":"{accepted}"}
,{"Name":"Carlos", "LastName":"Ramirez", "Address":"{Rejected}"}, ....... 22k more records here]</string>
I asked for some help earlier on here for the best way to do this, and i got a response about reading it using the xml parser and then a json parser, i am using GSON.
String XML = "<Your XML Response>";
XPathExpression xpath = XPathFactory.newInstance()
.newXPath().compile("/*[local-name()='string']");
String json = xpath.evaluate(new InputSource(new StringReader(XML)));
and then
JSONArray jsonRoot = new JSONArray(json.trim());
System.out.println(jsonRoot.getJSONObject(0).getString("Address")); // {accepted}
The problem with this is approach i am having is that it throws errors when reading the XML, it starts reading but after a while it stops with errors like:
java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractBuilder.java:94)
at java.lang.StringBuffer.append(StringBuffer.java:219)
at org.apache.harmony.xml.dom.CharacterDataImpl.appendData(CharacterDataImpl.java:43)
......
I would appreciate any advise on how to proceed with this, I am kind of new to android.
I don't know who would wrap 22k objects inside a xml string, but apparently someone is doing that. From my experience, your out of memory is because the you try to convert all the response to string but the response is too big to be handled. I recommend you to stream the JSON data. You can do stream the JSON data from the inputstream response that you get from the your HTTP post, but you need to skip the XML part by creating another input stream from the original response input stream and skip the XML part
Before I use the streaming API from google GSON I also got OOM error because the JSON data I got is very big data (many images and sounds in Base64 encoding) but with GSON streaming I can overcome that error because it reads the data per token not all at once. And for alternative you can also use Jackson JSON library I think it also have streaming API and how to use it almost same with my implementation with google GSON. I hope my answer can help you and if you have another question about my answer feel free to ask in the comment :)

How to get JSON content from a RESTful server to a Android client?

I've built a REST server that automatically retrieves data from my database and returns it as JSON. I want to know, how I can get this JSON content to my mobile and turn it into a string so that I can store this in the mobile's database.
First you need to be able to parse your JSON string. For a simple parser I gave an answer earlier here:
getting Json result in Android
Just parse through the list and add them to the database. There are different database tutorials out there.
Database tutorial: http://www.vogella.de/articles/AndroidSQLite/article.html
To make it as easy as possible try to find out small tutorials for different sub tasks like REST to JSON and database handler.
Here is a tutorial for JSON parsing from a REST webservice:
http://www.josecgomez.com/2010/04/30/android-accessing-restfull-web-services-using-json/
Here is another more simple JSON example:
http://inchoo.net/mobile-development/android-development/simple-android-json-parsing-example-with-output-into-listactivity/

Categories

Resources