问题
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..
回答1:
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)
来源:https://stackoverflow.com/questions/34759464/getjsonarray-in-graphresponse-cannot-be-applied-to-java-lang-string-facebook