I have a websocket server that is sending a JSON string to the users:
{
"message_type" : "draw",
"point": {
"color" : "#3b7bbf",
"width" : 20,
"x" : 191.98608,
"y" : 891.96094
},
"sender_id" : "123456abc"
}
I've created a Java Object to use for GSON to parse:
public class WsMessage {
private String message_type;
private String sender_id;
private Point point;
public WsMessage(String message_type, String sender_id, Point point) {
this.message_type = message_type;
this.sender_id = sender_id;
this.point = point;
}
public String getMessage_type() {
return message_type;
}
public String getSender_id() {
return sender_id;
}
public Point getPoint() {
return point;
}
@NonNull
@Override
public String toString() {
return new GsonBuilder().create().toJson(this, WsMessage.class);
}
}
However it's still giving me a com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
error on an incoming message.
Here is how I'm using the the GSON:
WsMessage wsMessage = gson.fromJson(text, WsMessage.class);
Log.d(TAG, "onMessage: Msg: " + wsMessage.toString());
I know it's something wrong with the way my WsMessage
class is structured, but i'm unsure what.
Edit:__________________________________________
Here is my Point
class:
public class Point {
private float x;
private float y;
private String color;
private int width;
private Point() {
}
public Point(float x, float y, String color, int width) {
this.x = x;
this.y = y;
this.color = color;
this.width = width;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public String getColor() {
return color;
}
public int getWidth() {
return width;
}
@Override
public String toString() {
return new GsonBuilder().create().toJson(this, Point.class);
}
}
Edit 2:__________________________________________________
After many hours of debugging... it turned out to be an issue from the server side adding a string in front of the JSON object and I didn't notice it because I was printing it out using log.d. There was no issue with the GSON converting the json string.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…