There is an idea, not new, but interesting in the implementation itself.

There is a blog (on wordpress) when adding news, automatically (using a plugin) news is duplicated in vk.

And actually the question is how to implement this, but in the android app. Create a fragment, shove a recyclerView into it and try using JSON or is it better to retrofit? I am familiar with both methods in absentia, and therefore I am not sure that this is possible (I know that it is possible :)). It was interesting to hear your opinion, perhaps even useful links with similar examples.

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants Vladyslav Matviienko , cheops , aleksandr barakin , Denis , rjhdby Oct 5 '16 at 18:15 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • retrofit and JSON are not opposites, I very simply work with retrofit with JSON. More precisely, the retrofit works instead of me, giving away immediately the data model. - Vladyslav Matviienko
  • The title of your question does not coincide with the question itself. - Vladyslav Matviienko
  • @metalurgus as far as I understand it is not a problem to make several different fragments and transfer content from different categories there? - Inkognito
  • If the content is the same format, then you can use the same class, but upload different data - Vladyslav Matviienko
  • one
    I did not understand anything from the previous comment - Vladyslav Matviienko

1 answer 1

In short, you create a Fragment in it with a RecyclerView. You get data from OkHttp or Retrofit based on the fact that it will be more convenient. The data will most likely be in JSON, it is possible in XML, but in JSON it will be simpler.

You can OkHttp values ​​through OkHttp , you can through Retrofit (which is the same OkHttp itself uses). The question is how the API is written. If there you have a trendy REST - no questions Retrofit. If you stupidly post the data back and forth, then it's easier OkHttp . Both have both a synchronous type of requests, and asynchronous. There is not much difference, in the asynchronous write a little less. Both there and there are 2 callbacks for a successful query result and a failure.

The basic data you have come most likely in the form of JsonArray . If you do not want to produce noodles, then it is best to immediately turn them into an ArrayList<YourObjectType> . The easiest way to do this is through the GSON library. It is generally used by default in Retrofit . You can always do the same in OkHttp . You can parse with standard tools, it will be a little longer, the result will be the same.

The resulting ArrayList<YourObjectType> is passed to the custom adapter for the RecyclerView , create a ViewHolder and in fact everything.

An example of working with RecyclerView

An example of working with OkHttp

Retrofit example

And now the fun part. It is worth deciding how exactly the application should find out that you have got some news there. Options 3:

1) The user opens the application - the data is loaded from the server by post.

2) The application has a service that periodically receives data from the server by post. Periodically - either at a particular time, or once at a certain time interval. Plus, before the first option - you can throw a Push notification.

3) The application has a service that the socket receives data about the receipt of news. Pros - instantly. Cons - you will have to create a server that will receive data from the site and push it to all connected clients. And of the downsides is hellish devouring the user's battery charge.

Update

Of all the options, I recommend making the first option, it is still needed and it will still come in handy. Tighten the service always have time.

Another important thing that did not immediately take into account - it is necessary to use the SQLite database to save already open news. Otherwise, without the Internet, the application will become completely useless.

Service example

SQLite Example

  • Thank you, quite an exhaustive answer, but regarding the 3 options for receiving the news (namely, the first two), it is possible, and there are additional tips (examples are generally super). - Inkognito
  • @Inkognito updated the post - DEADMC