I want to sync my SQLite database on android device with SQL Server Data. I am using Webservice developed in Visual Studio.
The Webservice is returning XML which I am getting from
client.get("http://10.0.2.2/WebApi/api/mpapi/GetAllWriting", params, new AsyncHttpResponseHandler() {
Now I am passing the response to extract JSON array from it.
Gson gson = new GsonBuilder().create();
try {
// Extract JSON array from the response
JSONArray arr = new JSONArray(response);
System.out.println(arr.length());
I am getting the following error.
Method threw 'java.lang.NullPointerException' exception. Cannot evaluate org.json.JSONArray.toString()
The value in response is
[{"Id":1,"Title":"Danish","Des":"Khan"},{"Id":2,"Title":"Rayan","Des":"Linkon"}]
What is the error is the XML not formatted properly? DO I need to change the Webservice./ Please guide as I have mostly copied the coding from multiple locations.
From your question it doesn't seems that you are getting xml data from your web service, however if in case you are sure that the response is xml you can try this Download java-json.jar. Add it into your library folder and get json object by executing the following code.
JSONObject jsonObj = null;
jsonObj = XML.toJSONObject(yourXmlObject);
You will need to import org.json.JSONObject; org.json.JSONException; org.json.XML; in order to execute above code.
Related
I'm trying to parse a JSON string, but getting an error when trying to get a nested object:
JSONObject jsonObject = new JSONObject(jsonString);
System.out.println(jsonObject);
System.out.println(jsonObject.keySet());
System.out.println(jsonObject.getJSONObject("matches"));
Below is the output in console. As far as I can see, the JSON is valid as jsonObject is created without an error. But when I try to obtain "matches" it throws an error. I've compared my code with tutorials but I can't see what the issue out to be:
{"matches":[{"id":233028,"awayTeam":{...
[matches, count, filters, competition]
Error in client: JSONObject["matches"] is not a JSONObject.
Anything I'm doing wrong? Happy to provide any further info if needed.
matches is an array, not an object. Use getJSONArray:
System.out.println(jsonObject.getJSONArray("matches"));
(Or more usefully:
System.out.println(Arrays.deepToString(jsonObject.getJSONArray("matches")));
since System.out.println on an array doesn't really show useful information on its own.)
I tried uploading an excel file and it worked well but the return message doesn't show. For the return message I use the below code as shown here:
JSONObject myObj = new JSONObject();
//sets success to true
myObj.put("success", true);
//convert the JSON object to string and send the response back
out.println(myObj.toString());
out.close();
For using this class I used the json-lib.jar. Further it asks for dependent jars like ezmorph-1.0.jar which I am doubtful of using.
Has anyone used the above method to return a message to the front end?
If so, what were the jars used in the process? Please help
I resolved the above by using json-simple-1.1.jar instead of json-lib.jar.
This created a JSONObject without the need of any other jar.
i m trying to make app that can communicate with mysql (localhost)
Only problem is Eclipse is showing JSONParser Error
After trying to find solution for error i found this Tutorial jackson library tho http://jackson.codehaus.org/
i could not found any solution after 2 days digging into android code
JSONParser jParser = new JSONParser();
ERROR: Multiple markers at this line
- JSONParser cannot be resolved
to a type
- JSONParser cannot be resolved
to a type
and this line of code has same error showing
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
Please help me so i can continue my app
help will highly appreciate!
thanks
If you need to parse a JSON string, you do not need to use any JSONParser class. The JSONObject itself can be used for parsing JSON.
Pass the JSON string to a new JSON object as following:
JSONObject responseObject = new JSONObject(response); //response is the JSON string that you get as response
String name = responseObject.get("name");
Similarly you can get the values directly from the responseObject itself. No need to use any third party libraries. You can find a very good documentation here.
EDIT:
I prefer Android Asynchronous Http Client for making web requests.
I've build a web application that produces XML code of an object. To my suprise, the xml produced is completely correct and in the format I wanted it. However, I'm now making a method that consumes XML in the same format and turn it back in an object. How can I test if it is working?
I've tried using a REST extension in chrome that posts the exact same XML that my other method produces, but I get the error: "The server refused this request because the request entity is in a format not supported by the requested resource for the requested method." I've also tried putting breakpoints in my code and debugging it that way, but my breakpoints are never even reached.
#GET
#Produces(MediaType.TEXT_XML)
public week_program getXml() {
week_program weekProgram = new week_program();
return weekProgram;
}
#POST
#Consumes(MediaType.TEXT_XML)
public Response PostXml(week_program weekProgram) {
System.out.println(weekProgram);
return Response.status(Status.OK).entity(weekProgram).build();
}
How can I fix it, or even test correctly if it does actually work?
I would suggest using json instead of XML and Gson from Google.
Since json output is usually smaller than XML (fat free).
Object to JSON
DataObject obj = new DataObject();
Gson gson = new Gson();
String json_string = gson.toJson(obj);
JSON to Object
DataObject obj = gson.fromJson(json_string, DataObject.class);
Here's a tutorial. http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
I am writing java code trying to convert an IPentahoResultSet to JSON so I can send it to a server using apache commons httpclient. I could not find any way to convert this pentaho result set to JSON. Any help will be appreciated.
I have tried this code to serialize it, but it does not work. I think it is meant to serialize classes not resultsets.
import flexjson.JSONSerializer;
import org.pentaho.commons.connection.marshal.MarshallableResultSet;
.
.
.
IPentahoResultSet data;
//data will contain result of executing and MDX against Mondrian
MarshallableResultSet result = new MarshallableResultSet();
result.setResultSet(data);
JSONSerializer serializer = new JSONSerializer();
String json = serializer.deepSerialize( result );