简体   繁体   English

使用Gson库解析Facebook Json对象

[英]Parsing Facebook Json object using Gson library

I have been working around to parse the JSON objects returned by Facebook. 我一直在努力解析Facebook返回的JSON对象。 One of the libraries which I relied on is Gson. 我依赖的库之一是Gson。 The aim was to fill a ListView created in an android app with user's photos found in his profile under the photos of you section. 目的是用在用户个人资料中“您的照片”部分下的用户照片填充在android应用程序中创建的ListView。 Therefore, I have used the following Command: 因此,我使用了以下命令:

GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "/" + profile.getId() +     "/photos", null,
                    HttpMethod.GET, new GraphRequest.Callback() {

            @Override
            public void onCompleted(GraphResponse response) {
                jsonObject = response.getJSONObject();
                Log.d("JSON", "response is returned");
                communicator.openFragment(jsonObject);
            }
        }
        );
        // This is how to use the fields
        Bundle parameters = new Bundle();
        parameters.putString("fields", "images");
        request.setParameters(parameters);
        request.executeAsync();

The JSON Object was sent to the Main activity of the application was going to send the same JSON Object into another Fragment. JSON对象已发送到应用程序的Main活动,该活动将把相同的JSON对象发送到另一个Fragment。 My problem comes with parsing the JSON Object . 我的问题来自解析JSON对象

I have used 2 classes for this purpose: one of which is a Response class Here is the code: 为此,我使用了2个类:其中一个是Response类,下面是代码:

public class Response {

private PagingEntity paging;

private List<DataEntity> data;

public Response() {

}

public void setPaging(PagingEntity paging) {
    this.paging = paging;
}

public void setData(List<DataEntity> data) {
    this.data = data;
}

public PagingEntity getPaging() {
    return paging;
}

public List<DataEntity> getData() {
    return data;
}

public static class PagingEntity {

    private CursorsEntity cursors;
    private String next;

    public void setCursors(CursorsEntity cursors) {
        this.cursors = cursors;
    }

    public void setNext(String next) {
        this.next = next;
    }

    public CursorsEntity getCursors() {
        return cursors;
    }

    public String getNext() {
        return next;
    }

    public static class CursorsEntity {
        private String after;
        private String before;

        public void setAfter(String after) {
            this.after = after;
        }

        public void setBefore(String before) {
            this.before = before;
        }

        public String getAfter() {
            return after;
        }

        public String getBefore() {
            return before;
        }
    }
}

public static class DataEntity {
    private String id;

    private List<ImagesEntity> images;

    public void setId(String id) {
        this.id = id;
    }

    public void setImages(List<ImagesEntity> images) {
        this.images = images;
    }

    public String getId() {
        return id;
    }

    public List<ImagesEntity> getImages() {
        return images;
    }

    public static class ImagesEntity {
        private String source;
        private int width;
        private int height;

        public void setSource(String source) {
            this.source = source;
        }

        public void setWidth(int width) {
            this.width = width;
        }

        public void setHeight(int height) {
            this.height = height;
        }

        public String getSource() {
            return source;
        }

        public int getWidth() {
            return width;
        }

        public int getHeight() {
            return height;
        }
    }
}
}

The CustomAdapter that uses this class is as follows: 使用此类的CustomAdapter如下:

public class CustomAdapter extends BaseAdapter{
private List<Response.DataEntity> dataEntityList;
private Context context;

public CustomAdapter(Context context, List<Response.DataEntity> dataEntityList) {
    this.context = context;
    this.dataEntityList = dataEntityList;
}

@Override
public int getCount() {
    return dataEntityList.size();
}

@Override
public Object getItem(int position) {
    return dataEntityList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

public Object getImage(int imageSet) {
    return dataEntityList.get(imageSet).getImages()
            .get(dataEntityList.get(imageSet).getImages().size() - 1);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.each_item_list, parent, false);

    ImageView thumbnail = (ImageView) rowView.findViewById(R.id.thumbnail);
    TextView height = (TextView) rowView.findViewById(R.id.height);
    TextView width = (TextView) rowView.findViewById(R.id.width);

    Response.DataEntity.ImagesEntity item = (Response.DataEntity.ImagesEntity) getImage(position);
    String imageURL = item.getSource();

    Picasso.with(context).load(imageURL).into(thumbnail);
    height.setText(item.getHeight() + "");
    width.setText(item.getWidth() + "");

    return rowView;
}
}

Therfore, I was able in the parse the JSON object and extract the URLs of the images, but I don't know how get the rest of the images because of the pagination features in the returned JSON Objects by facebook. 因此,我能够解析JSON对象并提取图像的URL,但是由于facebook返回的JSON对象中的分页功能,我不知道如何获取其余图像。 The code for parsing is as follows: ( This was added in the onCreateView in the fragment ) 解析代码如下:( 这是在片段的onCreateView中添加的

gson = new Gson();
    String jsonFetched = jsonObject.toString();
    String jsonToPass = jsonFetched.replace("\\/", "/");
    response = gson.fromJson(jsonToPass, Response.class);
    adapter = new CustomAdapter(getActivity().getBaseContext(),    response.getData());
    listView.setAdapter(adapter);
    return view;

Please help me out how to parse the rest of the JSON Object, that is, automatically going through the "next" tag in the JSON object to get the next JSON Object and parse it and so on 请帮助我了解如何解析JSON对象的其余部分,即自动通过JSON对象中的“ next”标签来获取下一个JSON对象并对其进行解析等等。

Any Help Much APPRECIATED.... THANKS 非常感谢您的任何帮助。

To get the rest of the Images, here is my new code: 要获取其余图像,这是我的新代码:

final GraphRequest.Callback graphCallback = new GraphRequest.Callback(){
        @Override
        public void onCompleted(GraphResponse response) {
                Bundle parameters = new Bundle();
                parameters.putString("fields", "images");
                parameters.putString("limit", "50");
                jsonObject = response.getJSONObject();
                GraphRequest newRequest = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT);
                newRequest.setGraphPath("/" + profile.getId() + "/photos");
                newRequest.setCallback(this);
                newRequest.setParameters(parameters);
                newRequest.executeAsync();
        }
    };

Notice that the information found on facebook developers is not enough https://developers.facebook.com/docs/reference/android/current/class/GraphResponse/ Therefore, I have to add the GraphPath, CallBack and bundles parameter. 请注意,在Facebook开发人员上找到的信息还不够https://developers.facebook.com/docs/reference/android/current/class/GraphResponse/因此,我必须添加GraphPath,CallBack和bundles参数。

Finally, This GraphRequest.Call object is to be added the GraphRequest request object as follows: 最后,将此GraphRequest.Call对象添加到GraphRequest请求对象中,如下所示:

final GraphRequest request = new GraphRequest(
                AccessToken.getCurrentAccessToken(), "/" + profile.getId() + "/photos", null,
                HttpMethod.GET, graphCallback
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM