我正在尝试创建一个Retrofit函数来检索一个对象列表,但它不起作用。它从“getAnuncios.enqueue”直接跳到结尾,从不调用onResponse。 我记录了http活动,没关系(200),但我不知道为什么从不调用onResponse。
以下是代码:
package com.sa.santaajuda.fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.sa.santaajuda.R;
import com.sa.santaajuda.activities.LoginActivity;
import com.sa.santaajuda.activities.MainActivityCliente;
import com.sa.santaajuda.adapters.AnuncioServicoAdapter;
import com.sa.santaajuda.beans.AnuncioServico;
import com.sa.santaajuda.beans.Usuario;
import com.sa.santaajuda.services.AnuncioServicoService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by b1104586 on 12/09/2017.
*/
public class BuscarServicos extends Fragment {
private RecyclerView mRecycleView;
private ArrayList<AnuncioServico> listaAnuncios;
public BuscarServicos(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.activity_buscar_servicos, container, false);
mRecycleView = view.findViewById(R.id.recycler_anuncios_servico);
getAnunciosServico();
RecyclerView.LayoutManager layout = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL, false);
mRecycleView.setLayoutManager(layout);
return(view);
}
public void getAnunciosServico(){
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
logging.setLevel((HttpLoggingInterceptor.Level.BODY));
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
// add your other interceptors …
// add logging as last interceptor
httpClient.addInterceptor(logging); // <-- this is the important line!
//Instância o serviço que irá comunicar para obter os anuncios de serviço
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(AnuncioServicoService.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
AnuncioServicoService service = retrofit.create(AnuncioServicoService.class);
Call<List<AnuncioServico>> getAnuncios = service.listarAnuncios();
getAnuncios.enqueue(new Callback<List<AnuncioServico>>() {
@Override
public void onResponse(Call<List<AnuncioServico>> callback, Response<List<AnuncioServico>> response) {
if (response.isSuccessful()) {
//limpa a lista local
listaAnuncios.clear();
List<AnuncioServico> listaTemp = response.body();
listaAnuncios.addAll(listaTemp);
mRecycleView.setAdapter(new AnuncioServicoAdapter(listaAnuncios, getContext()));
if (listaAnuncios.isEmpty())
Toast.makeText(getContext(), "Nenhum anúncio de serviço para exibir!", Toast.LENGTH_SHORT).show();
}else{
System.out.println(response.body());
}
}
@Override
public void onFailure(Call<List<AnuncioServico>> call, Throwable t) {
Toast.makeText(getContext(), "Erro ao comunicar com o servidor!", Toast.LENGTH_SHORT).show();
t.printStackTrace();
}
});
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
}