[英]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? 我的问题是如何让这个Android应用程序实时更新它的数据与上传到网站的任何内容? How do I approach it, some kind of HTTP request or what? 我如何处理它,某种HTTP请求或什么? Thanks. 谢谢。
If you are reading data from a rest API then the best library to use is retrofit. 如果您正在从其他API读取数据,那么最好使用的库是改进。 Retrofit converts the REST API into a Java interface. Retrofit将REST API转换为Java接口。 The request method and relative URL are added with an annotation, which makes code clean and simple. 请求方法和相对URL添加了注释,这使代码干净简单。 With annotations, you can easily add a request body, manipulate the URL or headers and add query parameters. 使用注释,您可以轻松添加请求正文,操纵URL或标题并添加查询参数。
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. 使用Retrofit获取数据。 And for realtime sync you can use a background service that hits after a particular time. 对于实时同步,您可以使用在特定时间后点击的后台服务。
http://square.github.io/retrofit/ http://square.github.io/retrofit/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.