jean d'arme jean d'arme - 6 months ago 33
Android Question

Parse.com + Retrofit = "Unable to create converter for class City"

I'm trying to query Parse.com server, but I have some problems with converter that I don't even use and get this error:

java.lang.IllegalArgumentException: Unable to create converter for class my-app-package/City
. Am I missing something here? Retrofit documentation is not as easy to digest as I would like:

Calling class:

String BASE_URL = "https://api.parse.com/1/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.build();

EndpointApi service = retrofit.create(EndpointApi.class);

Call<City> call = service.getCityList(); //here is error
call.enqueue(new Callback<City>() {
@Override
public void onResponse(Call<City> call, Response<City> response) {
Log.i("asd", response.toString());
}

@Override
public void onFailure(Call<City> call, Throwable t) {

}
});


EndpointApi.class:

public interface EndpointApi {

@Headers({"X-Parse-Application-Id: ...",
"X-Parse-Client-Key: " + Constants.PARSE_CLIENT_KEY})
@POST("functions/listaMiastGps")
Call<City> getCityList();
}


City.class:

@ParseClassName(Constants.CITY_CLASS)
public class City extends ParseObject {

public City() {
}

public double getLat() {
return this.getParseGeoPoint("geo").getLatitude();
}

public double getLng() {
return this.getParseGeoPoint("geo").getLongitude();
}

public String getName(){
return this.getString("city");
}
}


JSON string I need to read data from:

{
"result": [
{
"ACL": {
"*": {
"read": true
}
},
"__type": "Object",
"city": "Kraków",
"className": "Cities",
"createdAt": "2016-07-08T10:44:30.191Z",
"geo": {
"__type": "GeoPoint",
"latitude": 50.0617021,
"longitude": 19.9370549
},
"objectId": "FHpUIN2kX0",
"updatedAt": "2016-07-08T10:47:18.561Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"__type": "Object",
"city": "Warszawa",
"className": "Cities",
"createdAt": "2016-07-08T10:45:04.219Z",
"geo": {
"__type": "GeoPoint",
"latitude": 52.2329379,
"longitude": 21.0611941
},
"objectId": "SrfxmqsuAS",
"updatedAt": "2016-07-08T10:46:57.098Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"__type": "Object",
"city": "Wrocław",
"className": "Cities",
"createdAt": "2016-07-08T10:45:13.980Z",
"geo": {
"__type": "GeoPoint",
"latitude": 51.1100065,
"longitude": 17.0325047
},
"objectId": "Mj0KRPWQgd",
"updatedAt": "2016-07-08T10:47:20.465Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"__type": "Object",
"city": "Słupsk",
"className": "Cities",
"createdAt": "2016-07-08T14:11:38.488Z",
"geo": {
"__type": "GeoPoint",
"latitude": 54.4606811,
"longitude": 16.9580958
},
"objectId": "9HkXFmuS8a",
"updatedAt": "2016-07-08T14:12:44.083Z"
},
{
"ACL": {
"*": {
"read": true
}
},
"__type": "Object",
"city": "Gdańsk",
"className": "Cities",
"createdAt": "2016-07-09T20:44:38.173Z",
"geo": {
"__type": "GeoPoint",
"latitude": 54.3612063,
"longitude": 18.5499435
},
"objectId": "OhbqXGzd5v",
"updatedAt": "2016-07-09T20:44:51.491Z"
}
]


}

Answer

You are using the REST API. City Should not extend from ParseObject.

You can use GSON (or another JSON library) to create a POJO from the REST API. Add the GSON converter:

compile "com.squareup.retrofit2:converter-gson:VERSION"

Add the GSON converter when creating Retrofit instance:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

The City class should have fields corresponding to the names of the JSON elements (the columns from the Parse database).