Facebook Pagination Error - android

After successfully receiving the first request for information through the Graph API, I attempt to get the next page through the provided public GraphRequest getRequestForPagedResults(PagingDirection direction) method in the com.facebook.GraphResponse class.
However, I keep on getting null as a result on the line JSONObject pagingInfo = graphObject.optJSONObject("paging"); despite the returned JSONObject looking like the JSON code below.
{
"id": "10100476747286781",
"posts": {
"data": [
{
"id": "123123123"
...
}
],
"paging": {
"previous": "https://graph.facebook.com/v2.5/10100476747286781/posts?limit=200&since=1448931408&access_token=CAAM6MhXVsZAYBAN0tW33gMbwnWhs9HtZChlqsGwjgoR2IB9kZCej3pLS8dZCIOSsufYlVlHtJdkOZAHpr0bsPtZAmfj6ZAiXQ9zTXTe9lUghAuXnSQhZBM6YQfRPy26UfXbp4IQe9gKhG50qUZCURtOFAral1NqO8aIoAZCpRZBthp435HCo4uiZA7LqOIK7vxyT6MJ7e3nzcHyOhBDSSaqWYm1L9xUGzmml8Gg6TCZAzUupZCZBwZDZD&__paging_token=enc_AdCrwK4mXgYPS2XHW9Vjgb0ydGnENZCVb8cdyRGdPidfcQAc1573AWMVKR0DNZBzQmxg5ndkZAHfZAvWSpK8UFcG2SBZA&__previous=1",
"next": "https://graph.facebook.com/v2.5/10100476747286781/posts?limit=200&access_token=CAAM6MhXVsZAYBAN0tW33gMbwnWhs9HtZChlqsGwjgoR2IB9kZCej3pLS8dZCIOSsufYlVlHtJdkOZAHpr0bsPtZAmfj6ZAiXQ9zTXTe9lUghAuXnSQhZBM6YQfRPy26UfXbp4IQe9gKhG50qUZCURtOFAral1NqO8aIoAZCpRZBthp435HCo4uiZA7LqOIK7vxyT6MJ7e3nzcHyOhBDSSaqWYm1L9xUGzmml8Gg6TCZAzUupZCZBwZDZD&until=1334148469&__paging_token=enc_AdBEPCJpDZALodXcvmWUJy4rV4mQlFsHFNI8qNlvvVXGYAcZAkB8ZB1i1LRVKZCJND6j71MrINp1FKUDTTgQPZCTEU2t7"
}
}
}
Why doesn't optJSONObject search deeper into the JSON tree?
Is there an interface which I can use to directly use the URL provided in the JSONObject data?
i.e.
"next": "https://graph.facebook.com/v2.5/10100476747286781/posts?limit=200&access_token=CAAM6MhXVsZAYBAN0tW33gMbwnWhs9HtZChlqsGwjgoR2IB9kZCej3pLS8dZCIOSsufYlVlHtJdkOZAHpr0bsPtZAmfj6ZAiXQ9zTXTe9lUghAuXnSQhZBM6YQfRPy26UfXbp4IQe9gKhG50qUZCURtOFAral1NqO8aIoAZCpRZBthp435HCo4uiZA7LqOIK7vxyT6MJ7e3nzcHyOhBDSSaqWYm1L9xUGzmml8Gg6TCZAzUupZCZBwZDZD&until=1334148469&__paging_token=enc_AdBEPCJpDZALodXcvmWUJy4rV4mQlFsHFNI8qNlvvVXGYAcZAkB8ZB1i1LRVKZCJND6j71MrINp1FKUDTTgQPZCTEU2t7"

Personally, from my experience its better to access Facebook using REST API's instead of using their official SDK. It gives me a lot more freedom on how to handle the request-response and skip over any limitations which are part of the SDK.
Additionally, I can use my own networking layer of Volley/OkHttp which makes handling requests and threads much more easier and efficient.

Related

APis Retrofit GSON: Datatypes changing all the time

I am using Retrofit with GSON for an app for a client, and I am having some trouble with some of my client APIs and I need to workaround this problem.
Let's say i have an API which gives me telephones:
{
"telephones": [
{"phoneNumber": "1234567890"},
{"phoneNumber": "2123456789"}
]
}
But my client decided if there is only one telephone i am sending you:
{
"telephones":
{"phoneNumber": "1234567890"}
}
And when there is no telephone:
{
"telephones": "No telephone Available"
}
Is there any workaround i can make with Kotlin to solve this datatype problem?
In iOS I could force them reimplementing the Coding method and force them to always have an array. Is it possible to do something similar in Kotlin?
This is a small example, since the original answer has between 600 and 1300 lines of JSON data.
This might work.
I have done this in many places in my app.
So, the first thing, let's say you receive multiple different telephone numbers in one JSON file and might look something like this.
install a plugin call JSON to Kotlin Class
once done, make a new file using "Kotlin data class file from JSON"
the plugin for me creates automatically appropriate files.
then I use the main file to capture the data, this has worked for me almost everytime.
And yes, my other answer was if you are doing everything manually, while retrieving the data make a data class such as:
This is just for explanation purposes.
//lets say your json looks something like this
"records": [
{
"id": "1",
"telephones": [
{"phoneNumber": "1234567890"},
{"phoneNumber": "2123456789"}
]
},
{
"id": "2",
"telephones":
{"phoneNumber": "1234567890"}
},
{
"id": "3",
"telephones": "No telephone Available"
}
]
my example code would look something like this.
data class records(
//some id
val id: Int,
var telephones: List<Long>
)
so, now check inside telephones how many elements are there and add them to the list one by one.

Get particular key value from json in android (kotlin)

I'm beginner in android (Kotlin).I am getting the Following json from the HTTP Response.
{
"status": 1,
"userDetails": [
{
"AL_ID": "2",
"User_Id": "admin",
"User_Password": "admin",
"Created_Date": "2020-07-30 11:23:55"
}
],
"lastlogin": "2020-08-20 12:29:47"
}
I want to get status value from this json. How to get it.
You can use the built-in JSONObject and do something as shown below.
val json = JSONObject(jsonString) // String instance holding the above json
val status = json.getInt("status")
Having said that, I'd recommend you to take a look at Gson (for JSON serialization and deserialization) and Retrofit (an HTTP client for Android).
I would suggest using Gson library too, for android development in kotlin.
Also I would suggest to have the same DATA classes in kotlin as in database. In that way you can map objects fromJson and toJson with one line of code.
I found article that explains in details how to use Gson library with kotlin.
https://medium.com/#hissain.khan/parsing-with-google-gson-library-in-android-kotlin-7920e26f5520

Retrofit to parse json with an indefinite number of object names

I'm using retrofit to handle rest-api calls.
I have a rest API that returns the following json
"MyObject": {
"43508": {
"field1": 4339,
"field2": "val",
"field3": 15,
"field4": 586.78
},
"1010030": {
"field1": 1339,
"field2": "val212",
"field3": 1,
"field4": 86.78
},...
}
Please notice that the object MyObject contains objects with a name that is actually an id.
For all the other rest APIs I'm using retrofit without problems.
In this case it seems not possible to use the standard approach: defining a class containing the fields expected in the response.
Is there a way to transform this json into a json containing an array of
{
"field1": xxx,
"field2": "yyy",
"field3": www,
"field4": zzz
}
Or is there a better way to deal with this problem without going back to "manually" parsing the json?
Try to use next approach:
public class Response {
Map<String, YourObject> MyObject;
// getter, setter
}
public interface GitHubService {
#GET("some_path")
Call<Response> listMyObjects();
}
All you objects will be parsed to Map. You can get the list of all ids via keySet() method or list all entries with entrySet().
Try putting the annotation, SerializedName(nameOfField) over the variable name.
#SerializedName("13445345")
MyObject object;
Well my idea is quite manual, but it should work. I will not copy and paste another person's answer here, so take a look at this answer to see how to loop through all of the myObject's keys. Then for each of those keys, make a new JSONArray and add key value pair of fieldX-valueX.
This is just a basic idea, since I think you can handle the code yourself, you seem like a guy who knows his way around the simple stuff.

How to make multiple requests from response with Volley?

I have been using Volley to make requests to my api. Everything has been going great so far. I deserialize the JSON response and cache the object into my db, then query my db to show the object's data. But, what if my response is something like this:
{
"Author": {
"name": "John Doe",
"Books": [
{
"url": "www.myapi.com/book/1"
},
{
"url": "www.myapi.com/book/2"
},
{
"url": "www.myapi.com/book/3"
}
],
"Articles": [
{
"url": "www.myapi.com/article/1"
}
]
}
}
The urls are the api endpoints to the actual objects. To get all of the information that I need for my views, I will have to do a for loop and make 4 more api requests to get by Books and Articles objects. I am not sure what the best way is to accomplish this. I can't query my database until the requests have completed, and there isn't a way to know when the last request has finished.
This seems common, but I haven't come across anything yet that deals with this type of situation. How can this be done?
The response is bad for UX because you need to request N times before you can show something to the user.
If you can change the response, I suggest you do it.
By the way, you can use EventBus to send an object to the main thread from a Volley thread, so you can update the UI each time you finish a request.

Django REST Framework: Directly display on results list in GenericView

I'm using djangorestframework to manage a REST API that connects an Android mobile app to my Django web application. I have a list of objects that I need to retrieve from the web app through the REST API, so far a generic ListCreateAPIView works fine.
However, what the REST API returns isn't exactly a list/array per se, but a JSON object containing metadata and the actual results list. Here's an example of the said output:
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"foo":"bar"
}
]
}
The problem is, my mobile app's REST client expects a JSON list/array, not the JSON object above. Is there a way to make my generic view remove the count, next, and previous metadata and just output the results list itself? I need an output like the following:
[
{"foo":"bar"},
{"foo":"something"},
{"foo":"another"}
]
Oh, and I'm not sure if this would be helpful, but I use Retrofit as a REST client for my Android app, which is supposed to connect to my web app's REST API.
This object that wraps the array is generated by the queryset paginator. If you disable pagination you will get the array. To disable pagination, set paginate_by to None:
class PaginatedListView(ListAPIView):
queryset = ExampleModel.objects.all()
serializer_class = ExampleModelSerializer
paginate_by = None

Categories

Resources