У меня проблема с получением JSON
из response.body()
из моего API .
Logcat показывает, что запрос выполнен успешно, но когда я хочу скопировать тело в мой объект модели, он становится пустым.
Я уже сократил JSON
только до одного ключа и значения (конец logcat), но даже тогда я не могу прочитать ответ.
Logcat:
D/OkHttp: <-- 200 http://10.0.2.2:8080/groups?page=1&limit=25 (33ms)
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 14 Jan 2019 19:31:10 GMT
D/OkHttp: [{"name":"Delfiny"}]
<-- END HTTP (20-byte body)
Класс модели:
public class GroupsResponseModel {
@SerializedName("name")
@Expose
private String name;
public GroupsResponseModel() {
}
public GroupsResponseModel(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ApiInterface:
@GET("groups")
Call<GroupsResponseModel> getGroups(@Query("page") int page,
@Query("limit") int limit);
Модификация и перехватчик:
public static Retrofit getClient()
{
HttpLoggingInterceptor interceptor = new
HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient().newBuilder().
addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws
IOException {
Request originalRequest = chain.request();
Request.Builder builder = originalRequest.newBuilder().header(Utils.getAuthKey(),
Utils.getAuthValue());
Request newRequest = builder.build();
return chain.proceed(newRequest);
}
})
.addInterceptor(interceptor)
.build();
retrofit = new Retrofit.Builder()
.baseUrl(Utils.getURL())
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
Вызов:
Call<GroupsResponseModel> call = apiInterface.getGroups(1, 25);
call.enqueue(new Callback<GroupsResponseModel>() {
@Override
public void onResponse(Call<GroupsResponseModel> call, Response<GroupsResponseModel> response) {
groupsResponseModel = response.body();
Toast.makeText(GroupActivity.this, "response: " + groupsResponseModel.getName(), Toast.LENGTH_LONG).show();
//groups = new ArrayList<>(Arrays.asList(groupsResponseModel.getName()));
groupAdapter = new GroupAdapter(groups);
recyclerView.setAdapter(groupAdapter);
}
@Override
public void onFailure(Call<GroupsResponseModel> call, Throwable t) {
}
});
Ожидаемый результат должен быть проанализирован.