1

我正在使用 facebook sdk 并尝试修改父函数中的变量(至少我会这么称呼它)。

如何修改变量“actor”(标记)?

public void getUserFeed(int limit) {
    String q = ""; // query taken out
     Bundle params = new Bundle();
     params.putString("q", q);
     Session session = Session.getActiveSession();
     Request request = new Request(session,
         "/fql",                         
         params,                         
         HttpMethod.GET,   
         new Request.Callback(){    
            public void onCompleted(Response response) {
                 GraphObject graphObject = response.getGraphObject();
                 if (graphObject != null) {
                     JSONObject jsonObject = graphObject.getInnerJSONObject();
                     try {
                      JSONArray array = jsonObject.getJSONArray("data");
                      for (int i = 0; i < array.length(); i++) {
                         String q = "" // query taken out
                         Bundle params = new Bundle();
                         params.putString("q", q);
                         String actor = ""; // Trying to access this variable *******
                         Session session = Session.getActiveSession();
                         Request request2 = new Request(session,
                             "/fql",                         
                             params,                         
                             HttpMethod.GET,   
                             new Request.Callback(){    
                                public void onCompleted(Response response) {
                                    GraphObject graphObject = response.getGraphObject();
                                     //String s = textViewResults.getText().toString();
                                     if (graphObject != null) {
                                         JSONObject jsonObject = graphObject.getInnerJSONObject();
                                         try {
                                          JSONArray array = jsonObject.getJSONArray("data");
                                          for (int i = 0; i < array.length(); i++) {
                                              JSONObject object = (JSONObject) array.get(i);
                                              actor = object.getString("name"); // Trying to access "actor" here *******
                                          }
                                         } catch (JSONException e) {
                                             e.printStackTrace();
                                         }
                                     }
                                }
                            });
                        }
                    }
                }
            }
        }
    );
}
4

1 回答 1

1

您只能访问嵌套内部类中的最终变量。诀窍是对对象的引用可能是最终的,但可以修改对象的状态。

你可以做类似的事情,

AtomicReference actorRef = new AtomicReference("");
....
     public void onCompleted(Response response) {
          ....
          actorRef.set(object.getString("name"));

这解决了你问的问题。但是,可能更好的方法是不使用 Request.Callback,而是在不使用 Callback 的情况下创建 Request,然后调用

request.executeAndWait() 

executeAndWait() 将返回一个 Response 对象,您可以使用它来填充 actor。

于 2013-03-05T06:00:28.587 回答