Android Data Fetch From Database Not Working (php + Json + Mysql)
Solution 1:
I got a comment on your code that might help. The following line is useless
JSONObjectobject = newJSONObject(json.toString());
as you can just use json
as your JSONObject
instead of creating a new one.
Solution 2:
In reply to a comment from Sarthak Garg I'm posting an example here of converting a String response to a Java object using Gson.
I'd personally take a different approach to making a call to your web service than you are but that's outside the scope of this discussion so I'll just say what I would do given your existing code sample.
The following line from your code sample gives you a json object which can easily be converted to a String.
JSONObject json = jParser.makeHttpRequest(url_readResult, "GET",param);
String myString = json.toString();
What Gson does for you is all of the tedious work of parsing the json and extracting the various parts you need from it. To understand the full capabilities of Gson you'll need to do some Googling, the sample I give you here is just one thing you can do with it.
What this sample does is creates a new Gson instance and then uses that instance to convert your json String to a POJO (Plain Old Java Object). Think of the YourPojo class as a template which you give to Gson to use in order to tell it what the incloming json will look like and what you'd like the resulting object to look like.
Gsongson=newGson();
YourPojopojo= gson.fromJson(myString, YourPojo.class);
The only extra work you have to do here is create the YourPojo class and thankfully there are tools out there which can help with that, e.g. this one
These tools will take a json sample you provide it with and generate a Pojo class or classes for you. All you need do is supply the class name.
If you've implemented this successfully you'll be able to call methods like pojo.getProducts()
and pojo.getSuccess()
on your pojo object.
Post a Comment for "Android Data Fetch From Database Not Working (php + Json + Mysql)"