无法得到Json的回应

时间:2017-06-28 15:53:14

标签: android json android-recyclerview retrofit2

我无法通过改造获得json响应。我记录了错误所在的行。

这是主要活动

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();
                     //THE ERROR IS IN THE NEXT LINE
                     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());
            }
        });
    }
}

这是DHE ADAPTER

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) {

        //List<Usuario> chefs=new ArrayList<>();
        Chef chefs = mLstChef.get(position);
        holder.nombreschefscerca.setText(chefs.getNombre());
        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;
    }

}

这是界面

public interface EndPointsInterface {

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

这是ResponseClass

public class ChefResponse          
{
    private Chef[] results;

    public Chef[] getresults(){
        return results;
    }
}

我想在应用程序中显示的json响应在Web服务中看起来像这样:

<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)

你的回复应该是这样的

 @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<>();
    EndPointsInterface apiService = Conexion.getClient().create(EndPointsInterface.class);
    Call<ChefResponse> call = apiService.GetChefsCercanos(5);
    call.enqueue(new Callback<ChefResponse>() {
        @Override
        public void onResponse(Call<ResponseBean> call, Response<ResponseBean> response) {
            data.clear();
            if(response.isSuccessful()) {
                data=response.body().getItems();
                mRVMainAdapter= new RVMainAdapter(data);
                rvMain.setAdapter(mRVMainAdapter);
            }
        }
        @Override
        public void onFailure(Call<ChefResponse> call, Throwable t) {
            Log.d("Error",t.getMessage());
        }
    });
}

创建响应类

public class ChefResponse {
    public ArrayList<Chef> getItems() {
        return items;
    }

    public void setItems(ArrayList<Chef> items) {
        this.items = items;
    }

    @SerializedName("items")
    @Expose
    private ArrayList<Chef> items = null;
}

和像这样的厨师课

public class Chef {
    public Owner getNombre_chef() {
        return nombre_chef;
    }

    public void setNombre_chef(Owner nombre_chef) {
        this.nombre_chef = nombre_chef;
    }

    public Owner getRating() {
        return rating;
    }

    public void setRating(Owner rating) {
        this.rating = rating;
    }

    @SerializedName("nombre_chef")
    @Expose
    private Owner nombre_chef;

    @SerializedName("rating")
    @Expose
    private Owner rating;
}

同样修改你的Json像这样

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