Reputation: 33
So im trying to request all the names and all the links to the photo from the facebook api. But I keep getting an error because async task is already running.
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();
}
}
I'm sure im doing this all wrong, how should I be approaching this?
Upvotes: 0
Views: 105
Reputation: 131
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();
This will get name and image from the post on wall...also remember to download the picasso lib and add it in your project.
hope this helps
Upvotes: 1