0

UPDATE: I am now sure that the problem is my retrofit call, it was retrieving the wrong data. I am testing the app with an android device, so localhost won't work. In my retrofit call, I've tried using my public ip and wifi ip (found using by seeing the properties of the wifi I'm connected to), yet the retrofit call is unable to get my JSON. Any ideas how to fix this?

For some reason, my JSON is considered as malformed by GSON, hence I used Gson.setLenient().

I checked my JSON with jslint and it is valid. In addition, I'm unsure why the first line is considered to be a string when it starts with {.

Why is my JSON malformed and why is it throwing this error?

JSON

{ "result": [ { "_id": "5924eea5bd50af38702c14ae", "name": "Stoney's Bread Company", "cuisineType": "Italian", "address": "325 Kerr Street, Oakville", "openTime": "0900", "closeTime": "2100", "lng": 43.443733, "lat": -79.68146, "__v": 0, } ] }

Retrofitcode

Gson gson = new GsonBuilder().setLenient().create();

final MapInterface retrofit = new Retrofit.Builder()
   .baseUrl("http://myComputerIP/places/")
   .addConverterFactory(GsonConverterFactory.create(gson))
   .build().create(MapInterface.class);

Call<Result> call = retrofit.getMongoosePlace("3333733", "-79.681460");


public Double getLat() {
return lat;
}

public void setLat(Double lat) {
this.lat = lat;
}

public Integer getV() {
return v;
}

public void setV(Integer v) {
this.v = v;
}

}
Matthew Francis
  • 670
  • 3
  • 10
  • 26
  • Please check this [question](https://stackoverflow.com/questions/11484353/gson-throws-malformedjsonexception) – Fady Saad May 24 '17 at 03:00
  • 2
    There is also a comma that is not needed towards the end: `"__v": 0,` – dat3450 May 24 '17 at 03:50
  • Can you show your Result class? – Hammad Akram May 24 '17 at 06:23
  • Thanks @dat3450, unfortunatley, it did not work. – Matthew Francis May 24 '17 at 20:54
  • @Hammad Akram, I Added my two POJOs to my post. – Matthew Francis May 24 '17 at 20:55
  • 1
    Your Retrofit Call is supposed to return a Retrieve instance to match the above JSON, not a Result instance. Also, are you sure the JSON you posted matches what Retrofit actually retrieved from your server? – BladeCoder May 24 '17 at 21:05
  • @BladeCoderI am sure the problem is my retrofit call, it was retrieving the wrong data. I am testing the app with an android device, so localhost won't work. In my retrofit call, I've tried using my public ip and wifi ip (found using by seeing the properties of the wifi I'm connected to), yet the retrofit call is unable to get my JSON (eg; new Retrofit.Builder().baseUrl("http://76.64.XXX.XX/places/")). Any ideas why? – Matthew Francis May 24 '17 at 21:31
  • To see exactly what Retrofit sees in the response, from the device, you can configure a Logging Interceptor: https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor You can also use Stetho. – BladeCoder May 24 '17 at 21:36
  • Check to make sure that your string is getting imported with the correct encoding. I had similar troubles and even though the string looked fine in a text editor, examining it in the debugger revealed some hidden characters – Jeeter May 25 '17 at 20:49

2 Answers2

1

Your JSON was actually Malformed at the last element of JsonObject you have placed a comma which is malformed JSON look at code below I corrected it

{
  "result": [
    {
      "_id": "5924eea5bd50af38702c14ae",
      "name": "Stoney's Bread Company",
      "cuisineType": "Italian",
      "address": "325 Kerr Street, Oakville",
      "openTime": "0900",
      "closeTime": "2100",
      "lng": 43.443733,
      "lat": -79.68146,
      "__v": 0
    }
  ]
}
Pratik Vyas
  • 644
  • 7
  • 20
0

Found my solution!

It was both an android and Express (server side languange) problem.

I added the port number, 3000 to the retrofit call:

.baseUrl("http://myComputerIP:3000/places/")

and I made Express listen to a specific ip using

app.listen(portNum, insertIP).

Matthew Francis
  • 670
  • 3
  • 10
  • 26