0
這是改裝代碼這不是JSON數組。 (改造,GSON)
Retrofit builder = new Retrofit.Builder()
.baseUrl(APIUrl.API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RestAPI restAPI = builder.create(RestAPI.class);
Call<JsonObject> call = restAPI.wiseSaying();
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
Log.d("response----", response.body().toString());
JsonArray jsonArray = response.body().getAsJsonArray();
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
}
});
{"phrase":"[{\"author\":\"abc\",\"say\":\"aaaaa\"},
{\"author\":\"def\",\"say\":\"sdffdf.\"},
{\"author\":\"gsdf\",\"say\":\"dsfdsfdsf.\"}]"}
它的響應。(日誌結果)我覺得JsonArray 但錯誤ㅠ_ㅠ
FATAL EXCEPTION: main
Process: com.dsm.wakeheart, PID: 28267
java.lang.IllegalStateException: This is not a JSON Array.
at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106)
at com.dsm.wakeheart.Fragment.WiseSayingFragment$1.onResponse(WiseSayingFragment.java:82)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我所用jsonprimitive而不是jsonarray,所以我沒有得到一個錯誤。爲什麼?