GraphResponse中的getJSONArray()不能应用于java.lang.String - Facebook [英] getJSONArray() in GraphResponse Cannot be applied to java.lang.String - Facebook

查看:146
本文介绍了GraphResponse中的getJSONArray()不能应用于java.lang.String - Facebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private void loadFriendsFromFacebook(final String str){
final AccessToken accessToken = AccessToken.getCurrentAccessToken();
new GraphRequest(AccessToken.getCurrentAccessToken(),str,null,HttpMethod.GET,
new GraphRequest.Callback(){
列表< String> s = new ArrayList();
列表< String> F = new ArrayList();
列表< String> G = new ArrayList();
String t =;
String u =;
int w = 0;
boolean x = false;
String [] y = new String [2];
String E =;
OnInviteListener onInviteListener = null;

public void onCompleted(GraphResponse response){
SharedPreferences.Editor edit = getSharedPreferences(friendsData,0).edit();

try {
int i;
JSONArray jSONArray = response.getJSONArray(data);
for(i = 0; i< jSONArray.length(); i ++){
this.s.add(jSONArray.getJSONObject(i).getString(Page.Properties.ID));
}
for(i = 0; i< jSONArray.length(); i ++){
this.G.add(jSONArray.getJSONObject(i).getJSONObject(Page.Properties。 PICTURE).getJSONObject( 数据)的getString( URL));
edit.putString(friendPhoto,jSONArray.getJSONObject(i).getJSONObject(Page.Properties.PICTURE).getJSONObject(data)。getString(url));
this.F.add(jSONArray.getJSONObject(i).getString(Page.Properties.NAME));
edit.putString(friendName,jSONArray.getJSONObject(i).getString(Page.Properties.NAME));
}
edit.commit();
for(i = 0; i< this.s.size(); i ++){
this.u + =((String)this.s.get(i))+, ;
}
JSONObject jSONObject2 = response.getJSONObject(paging);
if(jSONObject2.toString()。contains(next)){
this.t = jSONObject2.getString(next)toString();
} else if(this.s.size()< 1){
this.x = true;
}

} catch(Exception e){
System.out.println(Exception =+ e);
e.printStackTrace();
}

}
})。executeAsync();

}

其中response.getJSONArray(data);



数据是下划线的红色,这是click rover显示在graphrespone中的getJSONArray()不能应用于java.lang.sting ..



和response.getJSONObject(paging)中的相同错误;



任何人都可以告诉我代码中有什么问题?
它将不胜感激。

解决方案

从这里( https://developers.facebook.com/docs/reference/android/current/class/GraphResponse/ )我可以看到 getJSONObject() getJSONArray()根本没有参数。您应该使用这种方法从 GraphResponse 中检索相应的对象,一旦您有一个 JsonObject JsonArray 可以访问特定字段。



response.getJSONArray()将给你一个类型为 JSONArray 的对象。response.getJSONObject()将给你一个类型为<$ c的对象$ C>的JSONObject 。使用此对象,您可以使用 jsonObject.getString(user_id)或类似文件访问这些字段(请参阅docs.oracle.com/javaee/7/api/javax/json/ JsonObject.html)


i have develop a android application integrate with facebook login now i want to make facebook app invite function(invitable_friends api) but in my code i got red underline errors.

  private void loadFriendsFromFacebook(final String str) {
    final AccessToken accessToken = AccessToken.getCurrentAccessToken();
    new GraphRequest(AccessToken.getCurrentAccessToken(), str, null, HttpMethod.GET,
            new GraphRequest.Callback() {
                List<String> s = new ArrayList();
                List<String> F = new ArrayList();
                List<String> G = new ArrayList();
                String t = "";
                String u = "";
                int w = 0;
                boolean x = false;
                String[] y = new String[2];
                String E = "";
                OnInviteListener onInviteListener = null;

                public void onCompleted(GraphResponse response) {
                    SharedPreferences.Editor edit = getSharedPreferences("friendsData", 0).edit();

                    try {
                int i;
                JSONArray jSONArray = response.getJSONArray("data");
                for (i = 0; i < jSONArray.length(); i++) {
                    this.s.add(jSONArray.getJSONObject(i).getString(Page.Properties.ID));
                }
                for (i = 0; i < jSONArray.length(); i++) {
                    this.G.add(jSONArray.getJSONObject(i).getJSONObject(Page.Properties.PICTURE).getJSONObject("data").getString("url"));
                    edit.putString("friendPhoto", jSONArray.getJSONObject(i).getJSONObject(Page.Properties.PICTURE).getJSONObject("data").getString("url"));
                    this.F.add(jSONArray.getJSONObject(i).getString(Page.Properties.NAME));
                    edit.putString("friendName", jSONArray.getJSONObject(i).getString(Page.Properties.NAME));
                }
                edit.commit();
                for (i = 0; i < this.s.size(); i++) {
                    this.u += ((String) this.s.get(i)) + ",";
                }
                JSONObject jSONObject2 = response.getJSONObject("paging");
                if (jSONObject2.toString().contains("next")) {
                    this.t = jSONObject2.getString("next").toString();
                } else if (this.s.size() < 1) {
                    this.x = true;
                }

            } catch (Exception e) {
                System.out.println("Exception=" + e);
                e.printStackTrace();
            }

        }
    }).executeAsync();

}

where the response.getJSONArray("data");

data is underline red which is click rover shows getJSONArray() in graphrespone cannot be applied to java.lang.sting..

and same error in the response.getJSONObject("paging");

Can anyone please tell me what is wrong in the code? it will be appreciated..

解决方案

From here (https://developers.facebook.com/docs/reference/android/current/class/GraphResponse/) I can see that getJSONObject() and getJSONArray() do not have parameters at all. You should retrieve the object respective the array from the GraphResponse using this methods and once you have a JsonObject or JsonArray you can access specific fields.

response.getJSONArray() will give you an object of type JSONArray and response.getJSONObject() will give you an object of type JSONObject. Using this objects you can access the fields using jsonObject.getString("user_id") or similar (see docs.oracle.com/javaee/7/api/javax/json/JsonObject.html)

这篇关于GraphResponse中的getJSONArray()不能应用于java.lang.String - Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆