使用Gson库解析Facebook Json对象

问题描述:

我一直在努力解析Facebook返回的JSON对象。我依赖的一个图书馆是Gson。目的是在你的部分的照片下填充一个在Android应用程序中创建的ListView和在他的个人资料中找到的用户照片。因此,我用下面的命令:使用Gson库解析Facebook Json对象

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

JSON对象被发送到应用程序的主要活动是去同一个JSON对象发送到另一个片段。 我的问题来解析JSON对象

我已经使用2类用于此目的:其中之一是一个响应类下面是代码:

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; 
     } 
    } 
} 
} 

使用这个类的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 ,我能够解析JSON对象并提取图像的URL,但我不知道如何获取其余图像,因为Facebook返回的JSON对象的分页功能。 解析的代码如下:(这是在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; 

请帮我如何解析JSON对象的其余部分,即通过自动去在JSON对象中的“下一步”标签来获取下一个JSON对象,并对其进行分析和等等

任何帮助非常赞赏....谢谢

去获取图片的其余部分,这里是我的新代码:

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(); 
     } 
    }; 

请注意,在facebook开发人员发现的信息是不够的https://developers.facebook.com/docs/reference/android/current/class/GraphResponse/因此,我必须添加GraphPath,CallBack和bundles参数。

最后,这GraphRequest.Call目的是要添加的GraphRequest请求对象如下:

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