因此,我试图从Facebook API请求所有名称和所有指向照片的链接。
但是我一直收到错误消息,因为异步任务已经在运行。
public void getPages(){
AsyncTask<String, Void, String[]> GL = j.new GetLikes().execute(extras.getString("PIC"));
try {
likes = GL.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<Json.PI;i++){
final int j = i;
final Request photo = new Request(
null,
"/" + PhotoId,
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
GraphObject graphObject = response.getGraphObject();
JSONObject jsonObject = graphObject.getInnerJSONObject();
try {
PageInfo[j][1] = (String) jsonObject.get("picture");
Log.i("PICTURE", PageInfo[j][1]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
);
final Request photos = new Request(
null,
"/"+PageId+"/photos/uploaded",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
GraphObject graphObject = response.getGraphObject();
JSONObject jsonObject = graphObject.getInnerJSONObject();
try {
JSONArray array = jsonObject.getJSONArray("data");
JSONObject object = (JSONObject) array.get(0);
Log.i("PHOTOID", (String) object.get("id"));
PhotoId = (String) object.get("id");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("PNAME", String.valueOf(response));
photo.executeAsync();
}
}
);
Request PageID = new Request(
null,
"/"+PageId,
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
GraphObject graphObject = response.getGraphObject();
JSONObject jsonObject = graphObject.getInnerJSONObject();
try {
PageInfo[j][0] = jsonObject.getString("name");
Log.i("PNAME", PageInfo[j][0]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
photos.executeAsync();
}
}
);
PageID.executeAsync();
}
}
我确定我做错了所有事情,我应该如何处理呢?
最佳答案
Request req = new Request(
session,
"/{page-id}/feed",
null,
HttpMethod.GET,
new Request.Callback(){
public void onCompleted(Response response) {
try
{
GraphObject go = response.getGraphObject();
JSONObject jso = go.getInnerJSONObject();
JSONArray arr = jso.getJSONArray( "data" );
String name = " ";
String picture = " ";
for ( int i = (arr.length()-1); i >=0; i-- )
{
JSONObject json_obj = arr.getJSONObject( i );
name = json_obj.getString( "name");
picture = json_obj.getString("picture");
//...
}
txtMessage.setText(name);
//imgFeed is ImageView
Picasso.with(getActivity()).load(picture).into(imgFeed);
}
catch ( Throwable t )
{
t.printStackTrace();
}
}
});req.executeAsync();
这将从墙上的帖子中获取名称和图像...也请记住下载picasso lib并将其添加到您的项目中。
希望这可以帮助