How to Parse Facebook Data - android

I am having a few problems getting Facebook data to parse properly.
I am working on implementing part of an application to allow a user to select and use one of their own facebook photos within the app. I have gotten the facebook login/logout code working and I am currently getting the token (once logged in) in order to gather the users Album information. The permission set is also working nicely, however I am now stuck at trying to get the JSON information to parse correctly. Here is a sample snippet of the information I need to parse:
"data": [
{
"id": "3486732467234",
"from": {
"name": "Persons Name",
"id": "Persons ID"
},
"name": "Vacation",
"location": "City",
"link": "https://www.facebook.com/album.php?fbid=434235&id=324343&aid=2430",
"cover_photo": "3489234432",
"privacy": "everyone",
"count": 60,
"type": "normal",
"created_time": "2007-06-03T23:01:16+0000",
"updated_time": "2011-03-18T19:46:43+0000",
"can_upload": true
},
{
"id": "4043544665",
"from": {
"name": "Persons Name",
"id": "Persons ID"
},
"name": "Vacation 2",
"location": "City",
"link": "https://www.facebook.com/album.php?fbid=4043434665&id=508154335&aid=2555",
"cover_photo": "5434543",
"privacy": "everyone",
"count": 60,
"type": "normal",
"created_time": "2007-06-03T22:53:03+0000",
"updated_time": "2011-03-18T19:45:55+0000",
"can_upload": true
}],
...more paging JSON information ...}}
I need to be able to pull the album information in order to display the photo album names in a list (and use the ID in another query). Once I can get the albums to show, I would then use those IDs to perform another query to get the photos from that album. Again I can also get that information back, I am just not sure how to properly parse it.
Any useful tips that someone might have in being able to parse this into a listview would be greatly appreciated.
I need to target pulling the first ID and the Album name ("id": "3486732467234" and "name": "Vacation" from the first listing)

why don't you try with the json classes...
something like this:
jsonObject = new JSONObject(your_data);
jArray = jsonObject.getJSONArray("data");
for(int i =0;i<jArray.length();i++){
String name= jArray.getJSONObject(i).getString("name");
String location= jArray.getJSONObject(i).getString("location");
etc.

The new Android Facebook SDK 3.0 returns a Response object in the Response.Callback() listener when making Graph API calls. This response object can be used to create a GraphObject which can be used to get a JSON...ex:
Request graphRequest = Request.newGraphPathRequest(session, graphRequestString, new Request.Callback() {
#Override
public void onCompleted(Response response) {
//Create the GraphObject from the response
GraphObject responseGraphObject = response.getGraphObject();
//Create the JSON object
JSONObject json = responseGraphObject.getInnerJSONObject();
}
});

Related

How to post Json Array using retrofit in android?

I am not able to post Json array from my add to cart list using retrofit. User will choose the products and then add to cart. Ineed save that data using sqlite db. Final order confirm state - I want to send all products (list) to server like this format code. I searched on google and can't find any solution.
JsonArray datas = new JsonArray();
JsonObject object = new JsonObject();
[
{
"name": "Phone",
"quantity": 50,
"price": 30000
},
{
"name": "Computer",
"quantity": 10,
"price": 15000
}
]
this is very simple just do like this:-
#POST("yourApiEndPoint")
Call<YourPojoClass> yourMethodName(#Query("data") JSONArray jsonArray);

How to change this json object to json array to consume it with Volley?

I am trying to consume below record using JSON with Volley, but it is not a JSONArray, How can I change it to use it in :
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Mean while, The URL in the record seems not correct. How to remove extar forward slashes from url or I can use it to fetch the JPG image?
{
"ID": "131",
"Lead": "",
"Title": "\u06f2\u06f0 \u06af\u0644 \u0641\u0648\u0642 \u0627\u0644\u0639\u0627\u062f\u0647 \u0631\u0648\u0646\u0627\u0644\u062f\u0648 \u062f\u0631 \u0644\u06cc\u06af \u0642\u0647\u0631\u0645\u0627\u0646\u0627\u0646",
"ContentTime": "09:56",
"TypeContent": "public",
"PTime": "1395\/05\/09 - 11:47",
"Content": " <\/div>",
"Tags": "\u0641\u0648\u062a\u0628\u0627\u0644#\u0631\u0648\u0646\u0627\u0644\u062f\u0648#\u0648\u0631\u0632\u0634\u06cc#######",
"Price": "0",
"GID": "106",
"GTitle": "\u0648\u0631\u0632\u0634\u06cc",
"PicURL": "http:\/\/video.dmedia.ir\/images\/news\/131\/thumb_131.jpg",
"comment_count": "0",
"view_count": "0",
"Media": [{
"GID": "359",
"GType": "mp4",
"Title": "",
"URL": "http:\/\/video.dmedia.ir\/images\/news\/131\/media\/359.mp4",
"ADV": 0
}],
"smscontent": null,
"Rels": [{
"RelID": "130",
"RelTitle": " \u0645\u0647\u062f\u0648\u06cc: \u062f\u0648\u0633\u062a \u062f\u0627\u0631\u0645 \u0648\u0627\u0644\u06cc\u0628\u0627\u0644\u0645 \u0628\u0627 \u06cc\u06a9 \u062e\u0627\u0637\u0631\u0647 \u062e\u0648\u0628 \u062a\u0645\u0648\u0645 \u0634\u0648\u062f"
}],
"Comments": []
}
Solution:
We can make JSON object request and JSON array request:
I found this helpful.
http://www.androidhive.info/2014/09/android-json-parsing-using-volley/
hi there is one plugin Gson for android studio.You need to install.Then go to CTRL + insert.
You can create gson file.
Enter some name for java file.
Click that file then Paste you json data. Click ok.
You can see your created json to gson format.
thanks hope this will help you.
create model class for this data then use follwing code
ModelClass class=new GSON.fromJSON("your Data ", ModelClass.class )

How to write and read contents containing complex json data?

friends:
I get a response from remote server as follows on Android:
{
"items": {
"persons": [
{
"id": "200120",
"name": "Bill"
},
{
"id": "200121",
"name": "Jim"
}
],
"tasks": [
{
"id": "001",
"name": "Fetch ten books",
"deadline": "5:30 p.m.",
"scores": "10"
},
{
"id": "002",
"name": "Fetch thirty books",
"deadline": "5:30 p.m.",
"scores": "30"
}
],
"intro": "This is a funny game."
},
"otherObj": []
}
And I want to save it to phones. I do not think it a good choice to save it into databases. However it read not fast if put the response to SharedPreferences file. Is there any other way?
No need to save this data into database as these value are dynamic and will change time to time.
In your Activity, call the API and get JSON as response and then parse this JSON and show it on device.
To store it on device u can use Singleton class also.
For JSON parser refer to http://www.tutorialspoint.com/android/android_json_parser.htm
You can first save your data into objects and at last into Object Array and then Save to database.(If your data is dynamic it is better you only deal with Object not database)
For saving to database you can use
db.beginTransaction();
for(object : ObjectArray){
db.dboperation();
}
db.setTransactionSuccessful();
db.endTransaction();

Jackson deserializing JSON into multiple POJOs

Hi I have following JSON
{
"code": 0,
"response": {
"userObject": {
"User": {
"id": "355660",
"first_name": "Dummy",
"last_name": "dummy",
"email": "dumb#email.com",
"birthday": "2012-05-07",
"created": "2012-08-21 06:41:05",
"modified": "2012-08-21 06:41:05",
"image_url": null,
},
"Location": {
"id": "273550",
"name": "New York City",
"asciiName": "New York City",
"lat": "40.714272",
"lon": "-74.005966",
"geoname_modified": "2011-11-08 00:00:00",
"timeZone": "America/New_York",
"countryName": "United States",
"state": "New York",
"created": "2012-07-12 12:11:01",
"modified": "2012-08-20 14:27:24"
}
}
}
}
I have two classes, one each for Location and User
I know that I can get the objects if I create nested class like
response
->UserObject
*User
*Location
But i don't want to create two extra classes for UserObject and response just for wrapping the two POJO's .
Is there any simpler way to do it??
I am using Jackson Parser with Spring for android
You can also do it in two steps, if you really want to avoid throw-away classes, like:
JsonNode tree = mapper.readTree(...);
User user = mapper.treeToValue(tree.path("response").path("userObject").get("User"), User.class);
Location loc = mapper.convertValue(tree.path("response").path("userObject").get("Location"), Location.class);
but yeah I might go with silly struct-classes instead:
static class Response {
public UserObject userObject;
}
static class UserObject {
public Location Location;
public User User;
}
since it really isn't much more code.
Rather than creating classes you could create arrays or use hashmap. Personally, I would just create the classes. I think that this give you more flexibility in your app, and will allow you to work with the objects with less hassle. I know it takes time to set them up, but once you do that, you can use ArrayList and you can parse the JSON quite a bit easier.

Android get Facebook User Name from their ID number

Is it possible to get a Facebook user's name from their Facebook Id number with a graph call?
If so, does anyone know the call?
For example you can get a user's picture by making the following call
"http://graph.facebook.com/" + someUserId + "/picture"
Something like:
String response = facebook.request(userId);
JSONObject json = Util.parseJson(response);
String name = json.getString("name");
String username = json.getString("username");
Simply querying the Graph API with the user_id should be enough to get the name of the user -
http://graph.facebook.com/4
>>
{
"id": "4",
"name": "Mark Zuckerberg",
"first_name": "Mark",
"last_name": "Zuckerberg",
"link": "https://www.facebook.com/zuck",
"username": "zuck",
"gender": "male",
"locale": "en_US",
"updated_time": "2012-05-20T01:29:18+0000",
"type": "user"
}
You could drill down even more by adding a filter for the name field -
https://graph.facebook.com/4?fields=name
>>
{
"name": "Mark Zuckerberg",
"id": "4",
"type": "user"
}
https://graph.facebook.com/UserID.
eg:
https://graph.facebook.com/19292868552

Categories

Resources