I have tried to fetch all album IDs from facebook using the following code.
But only first 25 albums were returned. Did I miss something?
response = facebook.request("me/albums");
json = Util.parseJson(response);
albums = json.getJSONArray("data");
for (int i =0; i < albums.length(); i++) {
JSONObject album = albums.getJSONObject(i);
String album_name = album.getString("name");
String album_id = album.getString("id");
}
But only first 25 albums were returned. Did I miss something?
Yes – that 25 is the default limit parameter value for requests like this.
Either use the paging links to retrieve the next 25 and so on, or set a higher limit yourself.
https://developers.facebook.com/docs/reference/api/pagination/
I use following ways to get the rest of album list.
next_page_URL = null;
next = json.getJSONObject("paging");
next_page_URL = next.getString("next");
response = facebook.request("me/albums&"+next_page_URL);
Related
I need to get "examples" data that is an array inside of 'results'
JSON FILE
and append it to a string. What would be the easiest way to do that?
this is how i would get "definitions"
private JSONObject queryResults;
...
...
String finalResults = "";
JSONArray results = queryResults.getJSONArray("results");
for(int i = 0; i < results.length(); i++){
JSONObject item = results.getJSONObject(i);
finalResults += item.getString("definition") + " ";
}
As this topic is related to Android application, the easiest way to get the mentioned value without using any additional libraries is to check if JSONObject inside the for-loop has attribute definitions and get the value as a String.
You can go with it like this
String finalExamples = "";
JSONArray results = queryResults.getJSONArray("results");
for (int i=0; i < results.length(), i++) {
JSONObject item = results.getJSONObject(i);
if (item.has("examples")) {
examples += item.getString("examples");
}
}
I assumed that you don't want to update your result String value if examples is not available in the JSONObject.
If you want to handle other cases, for example when "examples" is available in the object but is null or is set to empty String you can use other methods of JSONObject to check it.
More info about working with JSONObjects you can find in the documentation
I have a ListView onto which I would like to load image from url where images names are an ID.jpg (eg: 1.jpg / 2.jpg / 3.jpg.. etc). The IDs are from the database, however the images loaded into my ListView for all rows is one image (1.jpg).
txtDate.setText(Request_Date.get(position));
txtTime.setText(Request_Time.get(position));
//ivImage.setImageResource(imgid[position]); <-- this works from drawable and not from database
Picasso.with(context).load(url_poster + request_eventID + ".jpg").into(ivImage);
The request_eventID are IDs from DB, this is my problem, it only returns 1 (and not the rest of the IDs) so therefore only 1.jpg is loaded into the ListView on all rows.
EDIT:
How I retrieve IDs
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("read_columns");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String request_date = jsonChildNode.optString("Date");
String request_time = jsonChildNode.optString("Time");
request_eventID = jsonChildNode.optString("EID");
arrRequest_Date.add(request_date);
arrRequest_Time.add(request_time);
arrRequest_EID.add(request_eventID);
Note that I can see all IDs if I do a System.out.println("IDs" +request_eventID)
Ok, just as I suspected, I played around with the code a bit and indeed get(position) was what I needed on the IDs.
Solution:
Picasso.with(context).load(url_poster + Request_EID.get(position) + ".jpg").into(ivImage);
Thank you all for responding.
Well I'm new to Android. I'm getting a JSON string from a remote URL.
[{"key":"myString1","val":"myValue1"},{"key":"myString2","val":"myValue2"},{"key":"myString3","val":"myValue3"},{"key":"myString4","val":"myValue4"},{"key":"myString5","val":"myValue5"}]
I just need to parse this JSON string & display all key-val pair. I tried something like below from one of the tutorial.
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(0); //This will take first pair.
But I don't know the syntax for iterating through whole json object. Any help would be appreciated. Thanks in Advance.
There's nothing special in it. You do it like iterating any other array.
Let's say you have two String arrays to be filled with values: String[] mKey, mValue
Reading from JSON array will be like:
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
mKey[i] = object.getString("key");
mValue[i] = object.getString("val");
}
In the below code, I can display the first returned JSON place name result from my Google Places URL. Rather than just the first item, I'd like to display the whole returned list of place names. What would be the easiest way of doing this? Can you point me to a tutorial, or give sample code?
HttpGet get = new HttpGet(bcURL.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if(status == 200){
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
json = new JSONObject(data);
JSONArray timeline = json.getJSONArray("results");
JSONObject lastport = timeline.getJSONObject(0);
return lastport;
}else{
Toast.makeText(NewGPS3.this, "oops", Toast.LENGTH_SHORT);
return null;
}
The line JSONObject lastport = timeline.getJSONObject(0); shows the first name. I'd like to show a list rather than just the first item.
You are going to need to use a JSONArray object in order to get at the array. It should go something like this:
JSONObject entireObject = new JSONObject("your JSON string here");
JSONObject mainObject = entireObject.getJSONObject("photos"); // main object in entire object
JSONArray arrayObject = mainObject.getJSONArray("photo"); // array object in the main object
You can then iterate through this array just like any other:
for (int i = 0; i < arrayObject.length(); i++) {
JSONObject photoData = arrayObject.getJSONObject(i);
String yourString = photoData.getString("id");
}
I got some of this out of Pro Android Media. There is a really good JSON api call example in this book. You can probably get the source code for free, too.
http://www.amazon.com/Pro-Android-Media-Developing-Smartphones/dp/1430232676/ref=sr_1_1?ie=UTF8&qid=1343062690&sr=8-1&keywords=android+media
for(int i = 0; i < timeline.size(); i++){
JSONObject lastport = timeline.getJSONObject(i);
//Save the item to a ArrayList and use that list for your ListAdapter.
googlePlaces.add(lastPort);
}
In my app, I get the user's friends with the following:
https://graph.facebook.com/me?fields=id,name&access_token=xxxx
The above is actually done by the Facebook Android SDK, so I'm actually creating the request like this:
Bundle params = new Bundle();
params.putString("fields", "name,id");
asyncFacebookRunner.request("me/friends", params, "GET", listener, null);
I parse the JSON response like this:
JSONObject data = Util.parseJson(response);
JSONArray friendsData = data.getJSONArray("data");
mDbAdapter.deleteAllFriends();
for (int i = 0; i < friendsData.length(); i++) {
JSONObject friend = friendsData.getJSONObject(i);
mDbAdapter.addFriend(friend.getString("name"),
friend.getString("id"));
}
The Util class is part of the Facebook Android SDK.
On my Android phone with wifi on, this takes about 15-20 seconds to send, retrieve, and parse the response (my account has about 370 friends). I've seen a few other apps that do it in about 2 seconds. What's slowing me down here?
The only little reason for a delay I think is you database adapter, i.e. adding items while parsing through the result.
To optimize a bit, you could remove some of the assignments in your loop and add the data to the database through a single request:
JSONObject json = Util.parseJson(response);
JSONArray friendsData = json.getJSONArray("data");
String ids[] , names[] = new String[friendsData.length()];
for(int i = 0; i < friendsData .length(); i++){
ids[i] = friendsData .getJSONObject(i).getString("id");
names[i] = friendsData .getJSONObject(i).getString("name");
}
//TODO Add data to your database