简体   繁体   中英

ANDROID: Reading data from a website in real-time

I'm working on an app, and I want the app to be able to read most, or all, of the data from a website (The website is primarily a music site where songs are uploaded for users to download via links). I want whatever data that is uploaded on the music site(eg songs uploaded) to be updated on the app.

My question is how do I get this android app to update it's data in realtime with whatever is uploaded to the website? How do I approach it, some kind of HTTP request or what? Thanks.

If you are reading data from a rest API then the best library to use is retrofit. Retrofit converts the REST API into a Java interface. The request method and relative URL are added with an annotation, which makes code clean and simple. With annotations, you can easily add a request body, manipulate the URL or headers and add query parameters.

For example

    public interface RetrofitInterface {

    // asynchronously with a callback
    @GET("/api/user")
    User getUser(@Query("user_id") int userId, Callback<User> callback);

    // synchronously
    @POST("/api/user/register")
    User registerUser(@Body User user);
}


// example
   RetrofitInterface retrofitInterface = new RestAdapter.Builder()
            .setEndpoint(API.API_URL).build().create(RetrofitInterface.class);

// fetch user with id 2048
  retrofitInterface.getUser(2048, new Callback<User>() {
      @Override
      public void success(User user, Response response) {

    }

    @Override
    public void failure(RetrofitError retrofitError) {

    }
});

Use Retrofit to obtain the data. And for realtime sync you can use a background service that hits after a particular time.

http://square.github.io/retrofit/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM