API 的 Slim 框架并使用 Retrofit 进行消费
Slim framework for API and consuming with Retrofit
我尝试 return 列表 "question" 使用 slim 框架并在我的 android 应用程序中使用它们进行改造。
$app->get('/questions', function() use ($app, $bdd, $logger) {
$stmt = $bdd->prepare('SELECT * FROM questions');
$stmt->execute();
$questions = $stmt->fetchAll(PDO::FETCH_ASSOC);
$app->render(200, $questions);
});
给予
{
"0":{
"id":"1",
"userID":"1",
"choice_1":"choice 1",
"choice_2":"choice 2",
"count_1":"213",
"count_2":"165",
"dateAdd":"2016-03-06"
},
"1":{
"id":"2",
"userID":"1",
"choice_1":"choice 1",
"choice_2":"choice 2",
"count_1":"0",
"count_2":"0",
"dateAdd":"2016-03-04"
},
"error":false,
"status":200
}
在我的改装 api 服务中:
@GET("questions")
Call<ArrayList<Question>> getQuestions();
并调用 :
APIService api = getRetrofit().create(APIService.class);
Call<ArrayList<Question>> call = api.getQuestions();
call.enqueue(new Callback<ArrayList<Question>>() {
@Override
public void onResponse(Response<ArrayList<Question>> response, Retrofit retrofit) {
questions.addAll(response.body());
}
@Override
public void onFailure(Throwable t) {
Log.d("QFragment", "loadData error" + t.getMessage());
}
});
但不正确,因为 api 给我对象列表,在我的应用程序中我需要一个数组:
java.lang.IllegalStateException:预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 BEGIN_OBJECT $
有帮助吗?
结果
好的,通过 Jackub 的回答,我解决了我的问题,只需将 registerTypeAdapterFactory 添加到我的 GsonBuilder() 即可仅获取有效负载,例如:
public class ItemTypeAdapterFactory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("payload")) {
jsonElement = jsonObject.get("payload");
}
}
return delegate.fromJsonTree(jsonElement);
}
}.nullSafe();
}
}
因为有键error
和status
所以数组索引被转化为键。
你可以这样做
$payload = ['payload' => $questuons];
$app->render(200, $payload);
并在 android 应用程序中调整您对响应的消费。
我尝试 return 列表 "question" 使用 slim 框架并在我的 android 应用程序中使用它们进行改造。
$app->get('/questions', function() use ($app, $bdd, $logger) {
$stmt = $bdd->prepare('SELECT * FROM questions');
$stmt->execute();
$questions = $stmt->fetchAll(PDO::FETCH_ASSOC);
$app->render(200, $questions);
});
给予
{
"0":{
"id":"1",
"userID":"1",
"choice_1":"choice 1",
"choice_2":"choice 2",
"count_1":"213",
"count_2":"165",
"dateAdd":"2016-03-06"
},
"1":{
"id":"2",
"userID":"1",
"choice_1":"choice 1",
"choice_2":"choice 2",
"count_1":"0",
"count_2":"0",
"dateAdd":"2016-03-04"
},
"error":false,
"status":200
}
在我的改装 api 服务中:
@GET("questions")
Call<ArrayList<Question>> getQuestions();
并调用 :
APIService api = getRetrofit().create(APIService.class);
Call<ArrayList<Question>> call = api.getQuestions();
call.enqueue(new Callback<ArrayList<Question>>() {
@Override
public void onResponse(Response<ArrayList<Question>> response, Retrofit retrofit) {
questions.addAll(response.body());
}
@Override
public void onFailure(Throwable t) {
Log.d("QFragment", "loadData error" + t.getMessage());
}
});
但不正确,因为 api 给我对象列表,在我的应用程序中我需要一个数组: java.lang.IllegalStateException:预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 BEGIN_OBJECT $
有帮助吗?
结果
好的,通过 Jackub 的回答,我解决了我的问题,只需将 registerTypeAdapterFactory 添加到我的 GsonBuilder() 即可仅获取有效负载,例如:
public class ItemTypeAdapterFactory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("payload")) {
jsonElement = jsonObject.get("payload");
}
}
return delegate.fromJsonTree(jsonElement);
}
}.nullSafe();
}
}
因为有键error
和status
所以数组索引被转化为键。
你可以这样做
$payload = ['payload' => $questuons];
$app->render(200, $payload);
并在 android 应用程序中调整您对响应的消费。