我有一个简单的应用程序,用户登录Facebook。
如何访问用户喜欢的网页?我特别希望得到这些页面的名称。
成功登录后,我会完全调用函数fetchLikeData()
。
我操纵JSON对象的方式有问题吗?
因为我的字符串数组likeTitles []保持为null。
public void fetchLikeData()
{
final GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response)
{
Log.v("JSON1",object.toString());
//Handling JSON object
try {
String likeID;
String likeCategory;
String likeName;
int likeCount;
//Getting likes object
JSONObject jsonObjectLikes = object.getJSONObject("likes");
//Put likes data into a JSON array
JSONArray jsonArrayLikes = jsonObjectLikes.getJSONArray("data");
//Create FacebookLike array to populate with data
likeList = new ArrayList<FacebookLike>();
for(int i = 0; i < jsonArrayLikes.length(); i++)
{
//Store every single like in a JSON object
JSONObject jsonObjectLike = jsonArrayLikes.getJSONObject(i);
//Get id and category and name for every like
likeID = jsonObjectLike.getString("id");
likeCategory = jsonObjectLike.getString("category");
likeName = jsonObjectLike.getString("name");
likeCount = jsonObjectLike.getInt("likes");
Log.v("likeID", likeID);
Log.v("likeCategory", likeCategory);
Log.v("likeName", likeName);
Log.v("likeCount", Integer.toString(likeCount));
if (likeCount>100 && count<10) {
//Add like info to the list
likeTitles[i] = likeName;
count++;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "likes{id,category,name,location}");
request.setParameters(parameters);
request.executeAsync();
}
答案 0 :(得分:1)
//Firstly you have to add the permission before login.
LoginButton loginButton.setReadPermissions("email,publish_actions,user_friends,user_likes");
// Initialize the ArrayList globally:
public List<String> list = new ArrayList<String>();
//并使用以下代码检索数据
public void fetchLikeData(){
GraphRequest data_request = GraphRequest.newMeRequest(
login_result.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject json_object,
GraphResponse response) {
try {
JSONArray posts = json_object.getJSONObject("likes").optJSONArray("data");
Log.e("data1",posts.toString());
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.optJSONObject(i);
String id = post.optString("id");
String category = post.optString("category");
String name = post.optString("name");
int count = post.optInt("likes");
Log.e("name -", id+" name -"+name+ " category-"+ category+" count-"+count);
list.add(name);
// Added All name of pages in the list
}
Log.e("list",list.toString());
// Print Array list
} catch(Exception e){
}
}
});
Bundle permission_param = new Bundle();
permission_param.putString("fields", "likes{id,category,name,location,likes}");
data_request.setParameters(permission_param);
data_request.executeAsync();
}