Retrofit OnResponse获取json错误

时间:2017-06-27 21:04:06

标签: android android-recyclerview retrofit retrofit2

我尝试使用改造将我的应用与网络服务连接起来。我想使用recyclerview显示一个列表。 日志显示以下消息:HostConnection :: get()已建立新的主机连接,但在OnResponse中出现错误,然后应用程序崩溃

这是主要活动

public class MainActivity extends AppCompatActivity {

    private RecyclerView rvMain;
    private RVMainAdapter mRVMainAdapter;

    private ArrayList<Chef> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        rvMain = (RecyclerView) findViewById(R.id.rv_prueba);
        rvMain.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        data= new ArrayList<>();
        mRVMainAdapter = new RVMainAdapter(data);
        rvMain.setAdapter(mRVMainAdapter);

        EndPointsInterface apiService = Conexion.getClient().create(EndPointsInterface.class);

        Call<ChefResponse> call = apiService.GetChefsCercanos(5);
        call.enqueue(new Callback<ChefResponse>() {
            @Override
            public void onResponse(Call<ChefResponse> call, Response<ChefResponse> response) {
                int statusCode = response.code();

                    data.clear();
                    ChefResponse jsonResponse = response.body();
                     //NEXT LINE IS THE ERROR
                     data = new ArrayList<>(Arrays.asList(jsonResponse.getresults()));
                     mRVMainAdapter= new RVMainAdapter(data);
                     rvMain.setAdapter(mRVMainAdapter);
            }

            @Override
            public void onFailure(Call<ChefResponse> call, Throwable t) {
                Log.d("Error",t.getMessage());

            }
        });

    }
}

这是界面

public interface EndPointsInterface {

    @GET("Chef/{idUser}")
    Call<ChefResponse> GetChefsCercanos(@Path("idUser") Integer id_usuario);
}

这是jsonResponse类

public class ChefResponse
        implements Parcelable
{

    private Chef[] results;

    public Chef[] getresults(){
        return results;
    }
    public ChefResponse(){}
    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeArray(this.results);
    }
    public ChefResponse(Parcel in) {
        this.results = in.createTypedArray(Chef.CREATOR);
    }

    public static final Parcelable.Creator<ChefResponse> CREATOR = new Parcelable.Creator<ChefResponse>() {
        @Override
        public ChefResponse createFromParcel(Parcel source) {
            return new ChefResponse(source);
        }
        @Override
        public ChefResponse[] newArray(int size) {
            return new ChefResponse[size];
        }
    };
}

这是适配器

public class RVMainAdapter extends RecyclerView.Adapter<RVMainAdapter.RVMainAdapterViewHolder> {

    private List<Chef> mLstChef = new ArrayList<>();

    public void add(Chef chef){
        mLstChef.add(chef);
        notifyItemInserted(mLstChef.size()-1);
    }

    @Override
    public RVMainAdapter.RVMainAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chef_item,parent,false);
        RVMainAdapterViewHolder rvMainAdapterViewHolder = new RVMainAdapterViewHolder(view);
        return rvMainAdapterViewHolder;
    }

    @Override
    public void onBindViewHolder(RVMainAdapter.RVMainAdapterViewHolder holder, int position) {

        Chef chefs = mLstChef.get(position);
        holder.nombreschefscerca.setText(chefs.getTipoServicio());
        holder.ratingchef.setText(chefs.getRating().toString());
    }

    @Override
    public int getItemCount() {return mLstChef.size();
    }

    static class RVMainAdapterViewHolder extends RecyclerView.ViewHolder{
        TextView nombreschefscerca;
        TextView ratingchef;
        public RVMainAdapterViewHolder(View itemView) {
            super(itemView);
            nombreschefscerca = (TextView) itemView.findViewById(R.id.tv_nombreChefCercano);
            ratingchef = (TextView) itemView.findViewById(R.id.tv_rating);
        }
    }

    public RVMainAdapter(ArrayList<Chef> chefs){
        this.mLstChef=chefs;
    }

}
我试图加载的json是这样的

<string xmlns="http://smartchef.org/">
[{"nombre_chef":"Cesar","rating":"3.5"},{"nombre_chef":"Bruno","rating":"3.8"},{"nombre_chef":"Gean","rating":"2.5"}]
</string>

1 个答案:

答案 0 :(得分:0)

您的json热门商品不是带有结果字段的对象,这是一系列厨师。这是这个数组本身。 所以你可以将json修改成这样的东西

{
     "results":[{"nombre_chef":"Cesar","rating":"3.5"},{"nombre_chef":"Bruno","rating":"3.8"},{"nombre_chef":"Gean","rating":"2.5"}]
}

或修改端点定义中的响应

@GET("Chef/{idUser}")
Call<List<Chef>> GetChefsCercanos(@Path("idUser") Integer id_usuario);