Reputation: 4460
I'm trying use Gson to use with a JSON return of a Web Service. That is JSON return this format:
{
"cod":999,
"msg":"User Data.",
"return":{
"ID":"74",
"name":"FERNANDO PAIVA",
"devices":[]
}
}
When I try use gson.fromJson(json_return, MyObject.class
doesn't works and thrown an exception.
I'm follow this user guide: https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson
How can I do this ?
I'm trying this.
public class User implements Serializable{
private static final long serialVersionUID = 1L;
@SerializedName("ID")
private Long id;
private String name;
//get and set
}
/** get informations of web service */
public static String get(String url){
String s = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(new HttpGet(url));
HttpEntity entity = httpResponse.getEntity();
if(entity != null){
s = EntityUtils.toString(entity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
/** return object User */
public User getObjectById(){
String url = "mydomain.com.br/[email protected]";;
String response = HttpClient.get(url);
Gson gson = new Gson();
ResponseUser ru = gson.fromJson(response, ResponseUser.class);
User u = ru.getUser();
return u;
}
public class ResponseUser {
@SerializedName("return")
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Exception
11-19 12:56:23.281: E/AndroidRuntime(1481): FATAL EXCEPTION: main
11-19 12:56:23.281: E/AndroidRuntime(1481): Process: br.com.package.myapp, PID: 1481
11-19 12:56:23.281: E/AndroidRuntime(1481): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.package.myapp/br.com.package.myapp.act.MainActivity}: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.os.Handler.dispatchMessage(Handler.java:102)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.os.Looper.loop(Looper.java:136)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-19 12:56:23.281: E/AndroidRuntime(1481): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 12:56:23.281: E/AndroidRuntime(1481): at java.lang.reflect.Method.invoke(Method.java:515)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-19 12:56:23.281: E/AndroidRuntime(1481): at dalvik.system.NativeStart.main(Native Method)
11-19 12:56:23.281: E/AndroidRuntime(1481): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.Gson.fromJson(Gson.java:803)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.Gson.fromJson(Gson.java:768)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.Gson.fromJson(Gson.java:717)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.Gson.fromJson(Gson.java:689)
11-19 12:56:23.281: E/AndroidRuntime(1481): at br.com.package.myapp.dao.UsuarioDAO.getObjectById(UsuarioDAO.java:46)
11-19 12:56:23.281: E/AndroidRuntime(1481): at br.com.package.myapp.act.MainActivity.onCreate(MainActivity.java:27)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.Activity.performCreate(Activity.java:5231)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-19 12:56:23.281: E/AndroidRuntime(1481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-19 12:56:23.281: E/AndroidRuntime(1481): ... 11 more
11-19 12:56:23.281: E/AndroidRuntime(1481): Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
11-19 12:56:23.281: E/AndroidRuntime(1481): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
11-19 12:56:23.281: E/AndroidRuntime(1481): ... 20 more
Upvotes: 1
Views: 2147
Reputation: 18741
First problem is that your User
class does not represent the JSON you're getting back from the web service, but it only represents the "return"
field inside the response...
Try creating another class that does represent the JSON response, something like:
public class Response {
@SerializedName("return")
private User user;
// getter & setter
}
And then
response = gson.fromJson(response, Response.class);
u = response.getUser();
Apart from that, you have to change the type of the field id
in your User
class to be a String
.
Gson is complaining because you tell it to parse the "ID"
into a Long
, that's why it says expected BEGIN_OBJECT but was a String
, because it finds a String
with value "74"
Upvotes: 1
Reputation: 194
try {
JSONObject responseObj = new JSONObject(content);
Gson gson = new Gson();
User user = gson.fromJson(responseObj.getJSONObject("return"), User.class);
} catch (JSONException e) {
// catch here
}
Assuming User class is setup properly
Upvotes: 4