Parse.com + Retrofit ="无法为班级城市创建转换器"

时间:2016-07-15 11:46:13

标签: android parse-platform retrofit2

我正在尝试查询Parse.com服务器,但我在使用转换器方面存在一些问题,我甚至不使用并收到此错误:java.lang.IllegalArgumentException: Unable to create converter for class my-app-package/City。我在这里错过了什么吗?改造文档并不像我想的那样容易消化:

致电课程:

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字符串我需要从以下位置读取数据:

{
"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"
    }
]

}

1 个答案:

答案 0 :(得分:1)

您正在使用REST API。 City不应延伸至ParseObject

您可以使用GSON(或其他JSON库)从REST API创建POJO。添加GSON转换器:

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

创建Retrofit实例时添加GSON转换器:

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

City类应该包含与JSON元素名称相对应的字段(Parse数据库中的列)。