JSON已成功解析但返回空数据

时间:2018-01-28 13:54:43

标签: android json android-fragments android-adapter

美好的一天,我正在观看一个Android教程。我跟着它直到它在教程中成功显示数据。但是当我运行我的应用程序时,它成功解析了JSON响应,但是当我尝试显示它时它返回一个空值。

这是我的代码

EstablishmentData.java

public class EstablishmentData implements Serializable {

    public String estabName;

    public String estabType;

    public String estabAddress;

    public String estabImage;

}

EstablishmentAdapter.java

public class EstablishmentAdapter extends RecyclerView.Adapter<EstablishmentAdapter.EstablishementViewHolder>{

    private Context context;
    private ArrayList<EstablishmentData> establishmentList;

    public EstablishmentAdapter(Context context, ArrayList<EstablishmentData> establishmentList){
        this.context= context;
        this.establishmentList= establishmentList;
    }

    @Override
    public EstablishementViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater= LayoutInflater.from(parent.getContext());
        View view= inflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);

        EstablishementViewHolder estabHolder = new EstablishementViewHolder(view);

        return estabHolder;
    }

    @Override
    public void onBindViewHolder(EstablishementViewHolder holder, int position) {

        EstablishmentData establishmentData= establishmentList.get(position);
        String image_url= "http://10.0.3.2/blowOut/" + establishmentData.estabImage;

        Picasso.with(context)
//                .load(establishmentData.estabImage)
                .load(image_url)
                .placeholder(R.drawable.blowout)
                .error(android.R.drawable.stat_notify_error)
                .into(holder.estabImage);

        holder.estabName.setText(establishmentData.estabName);
        holder.estabName.setTextColor(Color.rgb(255,0,0));

        holder.estabAddress.setText(establishmentData.estabAddress);
        holder.estabAddress.setTextColor(Color.rgb(255,0,0));

        Log.d("Estab Adapter","Establishment logo: " +image_url);
        Log.d("Estab Adapter","Establishment name: " +establishmentData.estabName);
        Log.d("Estab Adapter","Establishment address: " +establishmentData.estabAddress);
    }

    @Override
    public int getItemCount() {

        if(establishmentList != null){
            return establishmentList.size();
        }
        return 0;
    }

    //This is the ViewHolder class
    public static class EstablishementViewHolder extends RecyclerView.ViewHolder{

        public CardView cvItem;
        public ImageView estabImage;
        public TextView estabName;
        public TextView estabAddress;

        public EstablishementViewHolder(View itemView) {
            super(itemView);

            cvItem          = itemView.findViewById(R.id.cvItem);
            estabImage      = itemView.findViewById(R.id.estabImage);
            estabName       = itemView.findViewById(R.id.estabName);
            estabAddress    = itemView.findViewById(R.id.estabAddress);

        }
    }
}

EstablishmentFragment.java

public class EstablishmentFragment extends Fragment {

    final String TAG= "EstablishmentFragment";

    RecyclerView rvItem;
    CardView cvItem;

    public EstablishmentFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_establishment, container, false);

        rvItem= rootView.findViewById(R.id.rv_recycler_view_fragment_accounts); //fragment_establishment.xml-> rvItem
        rvItem.setHasFixedSize(true);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rvItem.setLayoutManager(llm);

        StringRequest stringRequest= new StringRequest(Request.Method.GET,  AppConfig.URL_ESTABLISHMENT,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, response);

                        ArrayList<EstablishmentData> establishmentData = new JsonConverter<EstablishmentData>()
                                .toArrayList(response, EstablishmentData.class);

                        EstablishmentAdapter adapter= new EstablishmentAdapter(getContext(), establishmentData);

                        rvItem.setAdapter(adapter);

                        adapter.notifyDataSetChanged();
                    }

                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if(error != null){
                            Log.d(TAG, error.getMessage());
                            Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_SHORT);
                        }
                    }
                }
        );
        MySingleton.getInstance(getContext()).addToRequestQueue(stringRequest);

        return rootView;
    }
}

JsonConverter.class

public class JsonConverter<T> {
    public JsonConverter() {
    }

    public ArrayList<T> toArrayList(String jsonString, Class<T> clazz) {
        GsonBuilder builder = new GsonBuilder();
        builder.setDateFormat("dd/MM/yy HH:mm:ss");
        Gson gson = builder.create();
        Type type = new JsonConverter.ListParameterizedType(clazz);
        ArrayList<T> list = (ArrayList)gson.fromJson(jsonString, type);
        return list;
    }

    public List<T> toList(String jsonString, Class<T> clazz) {
        List<T> list = this.toArrayList(jsonString, clazz);
        return list;
    }

    private static class ListParameterizedType implements ParameterizedType {
        private Type type;

        private ListParameterizedType(Type type) {
            this.type = type;
        }

        public Type[] getActualTypeArguments() {
            return new Type[]{this.type};
        }

        public Type getRawType() {
            return ArrayList.class;
        }

        public Type getOwnerType() {
            return null;
        }
    }

}

JSON

[{
"name": "Lechon-nan",
"type": "Lechon",
"address": "Lechon street",
"image": "jollibee.png"
}, {
"name": "Lechon",
"type": "Lechon",
"address": "Lechon",
"image": "mcdo.png"
}]

logcat的

Adapter: Establishment image: http://10.0.3.2/blowOut/null
Adapter: Establishment address: null
Adapter: Establishment name: null

1 个答案:

答案 0 :(得分:2)

在pojo类中使用相同的名称,就像在Json键中一样;

public class EstablishmentData implements Serializable {

    public String name;

    public String type;

    public String address;

    public String image;

}

喜欢这个。