JSON错误:profile_picture

时间:2016-03-24 07:36:54

标签: android json android-volley android-gridview

我有一个Web服务,它返回一个图像列表(个人资料图片)及其名称和ID号。一些成员没有个人资料图片。所以我从drawable添加默认图片。我正在使用volley post和pojo来获取json并在gridview中添加图片和名称。但它没有显示图像和名称,而是显示JSON错误消息“没有profile_picture的值”。 This是我的json结果。因此,非常感谢获取图像和名称的任何帮助。

Main fragment:

public class SearchName extends Fragment{

    private GridView mGridView;
    private GridItem newItem;
    private GridAdapter mGridAdapter;
    private ArrayList<GridItem> mGridData;
    public static final String KEY_USERID = "user_id", KEY_NAME = "search_text";
    private static final String FETCH_ALL_MEMBERS = "http://example.com/api/member/get-all/";
    String profile_picture = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.searchname, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mGridView = (GridView)view.findViewById(R.id.grid_view);

        displayLog();

        mGridData = new ArrayList<>();
        mGridAdapter = new GridAdapter(getContext(), R.layout.gridrow, mGridData);
        mGridView.setAdapter(mGridAdapter);



    }

    private void displayLog() {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, FETCH_ALL_MEMBERS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jObj = new JSONObject(response);

                            System.out.println(jObj);

                            String status = jObj.getString("status");

                            // Now check status value
                            if (status.equals("0")) {
                                Toast.makeText(getActivity(), "There was some error! Please try again.", Toast.LENGTH_LONG).show();
                            } else if (status.equals("1")) {


                                JSONArray result = jObj.getJSONArray("result");
                                for (int i = 0; i < result.length(); i++) {
                                    JSONObject json_data = result.getJSONObject(i);
                                    String name = json_data.getString("name");
                                    String user_id = json_data.getString("user_id");
                                    //String profile_picture = json_data.getString("profile_picture");

                                    newItem.setName(name);
                                    newItem.setUserId(user_id);
                                    if(json_data.has("profile_picture")){

                                        //String image_def = String.valueOf(R.drawable.default_profile);
                                        newItem.setImage(json_data.getString("profile_picture"));

                                    }else{

                                        Uri path = Uri.parse("android.resource://com.sam.fitlincsearch/" + R.drawable.default_profile);
                                        String image_def = path.toString();
                                        newItem.setImage(image_def);

                                    }


                                    mGridData.add(newItem);
                                }


                                mGridAdapter.notifyDataSetChanged();

                            } else {
                                // Error in login. Get the error message
                                String errorMsg = jObj.getString("error_msg");
                                Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            // JSON error
                            e.printStackTrace();
                            Toast.makeText(getActivity(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(KEY_USERID, "1");
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        requestQueue.add(stringRequest);
    }
}
Adapter:

public class GridAdapter extends ArrayAdapter<GridItem> {

    private Context mContext;
    int resource;
    private ArrayList<GridItem> mGridData = new ArrayList<GridItem>();

    public GridAdapter(Context mContext, int resource, ArrayList<GridItem> mGridData) {
        super(mContext, resource, mGridData);
        this.resource = resource;
        this.mContext = mContext;
        this.mGridData = mGridData;
    }

    public void setGridData(ArrayList<GridItem> mGridData) {
        this.mGridData = mGridData;
        notifyDataSetChanged();
    }

    @Override
    public GridItem getItem(int position) {
        return super.getItem(position);
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        ViewHolder holder;
        if (v == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();

                v = inflater.inflate(R.layout.gridrow, parent, false);
                holder.image = (ImageView) v.findViewById(R.id.item_image);
                holder.name = (TextView) v.findViewById(R.id.item_name);

            holder.image = (ImageView) v.findViewById(R.id.item_image);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }


            GridItem item = mGridData.get(position);
            Picasso.with(mContext)
                    .load(item.getImage())
                    .placeholder(R.drawable.placeholder)
                    .fit()
                    .into(holder.image);
            holder.name.setText(item.getName());


        return v;
    }

    class ViewHolder {
        ImageView image, icon;
        TextView name;
    }

}
Pojo:

public class GridItem {
    private String nameUrls, imageUrls, sportsCat, userId;

    public GridItem() {
        super();
    }

    public String getImage() {
        return imageUrls;
    }

    public void setImage(String imageUrls) {
        this.imageUrls = imageUrls;
    }

    public String getName() {
        return nameUrls;
    }

    public void setName(String nameUrls) {
        this.nameUrls = nameUrls;
    }

    public String getSportsCat(){
        return sportsCat;
    }

    public void setSportsCat(String sportsCat){
        this.sportsCat = sportsCat;
    }

    public String getUserId(){
        return userId;
    }

    public void setUserId(String userId){
        this.userId = userId;
    }
}

2 个答案:

答案 0 :(得分:1)

你需要像这样检查字符串

String profile_picture = "";
if(json_data.has("profile_picture")){
    profile_picture = json_data.getString("profile_picture");
}else{
    profile_picture = "";
}

你的newItem为空。

你需要在for循环中首先初始化它

newItem = new GridItem();

答案 1 :(得分:0)

尝试此操作,而不是检查配置文件图片是否为null,而不是检查JSON是否包含profile_picture,例如

  If (!json_data.has("profile_picture"))){

                                    //String image_def = String.valueOf(R.drawable.default_profile);
                                    Uri path = Uri.parse("android.resource://com.sam.fitlincsearch/" + R.drawable.default_profile);
                                    String image_def = path.toString();
                                    newItem.setImage(image_def);
                                }else{
                                    newItem.setImage(json_data.getString("profile_picture");
                                }