如何将 Facebook Graph api 的结果添加到 Java 中的数组列表
How to add results of Facebook Graph api to array list in Java
我正在使用 Facebook 图 api 来找出用户属于哪些页面。当查询返回一个 json 对象时,它有我需要的东西,但由于某种原因它不想添加到我的数组列表中。正确的值打印在 log.d 中,它似乎出于某种原因跳过了我的数组列表。有什么想法吗?
查找页面功能
private ArrayList<String> foundPages;
private JSONObject jsonObject;
public ArrayList<String> findPages()
{
accessToken = AccessToken.getCurrentAccessToken();
foundPages = new ArrayList<>();
GraphRequest request = GraphRequest.newGraphPathRequest(
accessToken,
"/me/accounts",
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
try {
jsonObject = response.getJSONObject();
for(int i=0; i < jsonObject.getJSONArray("data").length(); i++)
{
page = response.getJSONObject().getJSONArray("data").getJSONObject(i).getString("name");
Log.d("viewmodel",page);
foundPages.add(page);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
request.executeAsync();
return foundPages;
}
是的。这里是:
request.executeAsync();
触发异步请求。但是您的 "current" 线程只是继续做:
return foundPages;
它 returns 是一个空列表。
该列表稍后会被填充,但在该方法 returns 的那一刻,该列表仍然是空的。或者只是被填满。谁知道呢,因为它会在未来的某个未知时间被异步填充。
一个解决方案可能是让一些 other variable/field 告诉你数据已经到达并推入列表 .
或者,该方法可以只发出一个同步请求,在数据到达之前简单地阻止调用者继续进行。
你看,你不能两全其美:当你不等待结果到达时,你不应该期望它们立即可用。
有一个解决这个问题的常用方法,就是定义一个回调方法,在调用填充这些值之后,它会 return 给你,就像这样(我的java生锈了,忍忍...)
定义接口:
interface Callback{
void apiResponseCallback(ArrayList<Page> result);//whatever your model is, make the array of that type
}
然后,在您的常规 findPages 方法中,将其更改为:
public void findPages(Callback callback) {
//
//
........
for(int i=0; i < jsonObject.getJSONArray("data").length(); i++)
{
page = response.getJSONObject().getJSONArray("data").getJSONObject(i).getString("name");
Log.d("viewmodel",page);
foundPages.add(page);
}
callback.apiResponseCallback(foundPages);//here we are returning the data when it is done
}
然后,当你调用 findPages
findPages(new Callback() {
@Override
public void apiResponseCallback(ArrayList<Page> result) {
here, this result parameter that comes through is your api call result to use, so result will be your populated pages to use.
}
});
}
为了完整性:
public void findPages(Callback callback)
{
accessToken = AccessToken.getCurrentAccessToken();
foundPages = new ArrayList<>();
GraphRequest request = GraphRequest.newGraphPathRequest(
accessToken,
"/me/accounts",
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
try {
jsonObject = response.getJSONObject();
for(int i=0; i < jsonObject.getJSONArray("data").length(); i++)
{
page = response.getJSONObject().getJSONArray("data").getJSONObject(i).getString("name");
Log.d("viewmodel",page);
foundPages.add(page);
}
callback.apiResponseCallback(foundPages);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
request.executeAsync();
}
我正在使用 Facebook 图 api 来找出用户属于哪些页面。当查询返回一个 json 对象时,它有我需要的东西,但由于某种原因它不想添加到我的数组列表中。正确的值打印在 log.d 中,它似乎出于某种原因跳过了我的数组列表。有什么想法吗?
查找页面功能
private ArrayList<String> foundPages;
private JSONObject jsonObject;
public ArrayList<String> findPages()
{
accessToken = AccessToken.getCurrentAccessToken();
foundPages = new ArrayList<>();
GraphRequest request = GraphRequest.newGraphPathRequest(
accessToken,
"/me/accounts",
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
try {
jsonObject = response.getJSONObject();
for(int i=0; i < jsonObject.getJSONArray("data").length(); i++)
{
page = response.getJSONObject().getJSONArray("data").getJSONObject(i).getString("name");
Log.d("viewmodel",page);
foundPages.add(page);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
request.executeAsync();
return foundPages;
}
是的。这里是:
request.executeAsync();
触发异步请求。但是您的 "current" 线程只是继续做:
return foundPages;
它 returns 是一个空列表。
该列表稍后会被填充,但在该方法 returns 的那一刻,该列表仍然是空的。或者只是被填满。谁知道呢,因为它会在未来的某个未知时间被异步填充。
一个解决方案可能是让一些 other variable/field 告诉你数据已经到达并推入列表 .
或者,该方法可以只发出一个同步请求,在数据到达之前简单地阻止调用者继续进行。
你看,你不能两全其美:当你不等待结果到达时,你不应该期望它们立即可用。
有一个解决这个问题的常用方法,就是定义一个回调方法,在调用填充这些值之后,它会 return 给你,就像这样(我的java生锈了,忍忍...)
定义接口:
interface Callback{
void apiResponseCallback(ArrayList<Page> result);//whatever your model is, make the array of that type
}
然后,在您的常规 findPages 方法中,将其更改为:
public void findPages(Callback callback) {
//
//
........
for(int i=0; i < jsonObject.getJSONArray("data").length(); i++)
{
page = response.getJSONObject().getJSONArray("data").getJSONObject(i).getString("name");
Log.d("viewmodel",page);
foundPages.add(page);
}
callback.apiResponseCallback(foundPages);//here we are returning the data when it is done
}
然后,当你调用 findPages
findPages(new Callback() {
@Override
public void apiResponseCallback(ArrayList<Page> result) {
here, this result parameter that comes through is your api call result to use, so result will be your populated pages to use.
}
});
}
为了完整性:
public void findPages(Callback callback)
{
accessToken = AccessToken.getCurrentAccessToken();
foundPages = new ArrayList<>();
GraphRequest request = GraphRequest.newGraphPathRequest(
accessToken,
"/me/accounts",
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
try {
jsonObject = response.getJSONObject();
for(int i=0; i < jsonObject.getJSONArray("data").length(); i++)
{
page = response.getJSONObject().getJSONArray("data").getJSONObject(i).getString("name");
Log.d("viewmodel",page);
foundPages.add(page);
}
callback.apiResponseCallback(foundPages);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
request.executeAsync();
}