-1

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.

DIRTY DAVE
  • 921
  • 1
  • 6
  • 30

3 Answers3

-1

Create your response Model class using Gson like this,

Here is Your WsMessage Class,

public class WsMessage {

@SerializedName("message_type")
@Expose
private String messageType;
@SerializedName("point")
@Expose
private Point point;
@SerializedName("sender_id")
@Expose
private String senderId;

public WsMessage(String messageType, Point point, String senderId) {
this.messageType = messageType;
this.point = point;
this.senderId = senderId;
}

public String getMessageType() {
return messageType;
}

public void setMessageType(String messageType) {
this.messageType = messageType;
}

public Point getPoint() {
return point;
}

public void setPoint(Point point) {
this.point = point;
}

public String getSenderId() {
return senderId;
}

public void setSenderId(String senderId) {
this.senderId = senderId;
}

}

Here is Your Point class,

public class Point {

@SerializedName("color")
@Expose
private String color;
@SerializedName("width")
@Expose
private Integer width;
@SerializedName("x")
@Expose
private Double x;
@SerializedName("y")
@Expose
private Double y;

public Point(String color, Integer width, Double x, Double y) {
this.color = color;
this.width = width;
this.x = x;
this.y = y;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public Integer getWidth() {
return width;
}

public void setWidth(Integer width) {
this.width = width;
}

public Double getX() {
return x;
}

public void setX(Double x) {
this.x = x;
}

public Double getY() {
return y;
}

public void setY(Double y) {
this.y = y;
}

}

Now you can parse any value from WsMessage class,After API calling print your WsMessage Using Gson lib.like this,

Log.d(TAG, "onMessage: Msg: " + new Gson().toJson(wsMessage));
Ronak Patel
  • 393
  • 3
  • 18
-1

Try this parser.

public WsMessage parseWsMessage(String json) {
    WsMessage result = null;
    try {
        Type itemsMapType = new TypeToken<WsMessage>() {
        }.getType();
        result = new Gson().fromJson(json, itemsMapType);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return result;
}
abdiwan
  • 39
  • 4
-1

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 when printing it out using log.d. There was no issue with the GSON converting the json string.

DIRTY DAVE
  • 921
  • 1
  • 6
  • 30