0

I am new to Android development. I have a node test server running that returns the following json post response

res.json({ userName: 'test@gmail.com', ratedOn: new Date(), rating: Math.ceil(Math.random() * 5) });

I am using gson to parse the response into an object Class Definition of the target class

public class RateWork {
    public int rating;
    public Date ratedOn;
    public String userName;

    public RateWork(int rating, Date ratedOn, String userName){
        this.rating = rating;
        this.ratedOn = ratedOn;
        this.userName = userName;
    }
}

In the client(Android side) i am using volley to capture the response.

StringRequest jsonObjReq = new StringRequest(Request.Method.POST, Constants.API_POST_RATING,                                    

new Response.Listener<String>() {    
    @Override
    public void onResponse(String response) {
        Log.d("TEST", response);
        RateWork rw = gson.fromJson(response, RateWork.class);
    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d("TEST", "Error: " + error.getMessage());
    }
}

Log of the response

D/TEST: {"userName":"test@gmail.com","ratedOn":"Sat Dec 09 14:10:49 GMT+05:30 2017","rating":3}

I get an exception at this line -> com.google.gson.JsonSyntaxException

RateWork rw = gson.fromJson(response, RateWork.class);

Please help fix the error.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Navaneeth
  • 190
  • 1
  • 1
  • 16

1 Answers1

0

Use this to create your Gson Object

Gson gson = new GsonBuilder()
   .setDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").create();

refer this for more detail

Gautam Chibde
  • 1,167
  • 3
  • 14
  • 27