[英]How to access an API which is having high response time in android app?
I have a java API that is taking around 50 sec to return the response and improvement in API is not possible. 我有一个Java API,大约需要50秒才能返回响应,因此无法改善API。 User can not proceed in next page until the response is complete and it is kind of blocker in the android app which is using the API.
在响应完成之前,用户无法进入下一页,这是使用API的android应用中的一种阻止程序。 Please suggest some alternate approach on this.
请为此提出一些替代方法。
For example: we can have two table and API will return the data from table instead waiting for the result of query which is taking more time and we can spawn a thread to run the query in the background and update the table data with the result. 例如:我们可以有两个表,API将从表中返回数据,而不是等待查询结果,这会花费更多时间,我们可以生成一个线程在后台运行查询并使用结果更新表数据。 Based upon time stamp in response data android app can know the data is old or latest and in case of old data they can request it again after some time to get the latest data.
根据响应数据中的时间戳,Android应用程序可以知道数据是旧数据还是最新数据,如果是旧数据,他们可以在一段时间后再次请求获取最新数据。
Please let me know if the question or approach is not clear. 如果问题或方法不清楚,请告诉我。
There is specific way of doing such things. 有做这种事情的特定方法。 There is a thing called
Loader
in android. android中有一个叫做
Loader
的东西。 CursorLoader
is used for content providers. CursorLoader
用于内容提供程序。 and AsyncTaskLoader
for api calls 和
AsyncTaskLoader
用于api调用
Try this 尝试这个
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(httpLoggingInterceptor);
}
also add this dependencies in gradle app level 还在gradle应用程序级别添加此依赖项
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
Smaple response 枫反应
Date: Fri, 12 Aug 2016 07:33:23 GMT
OkHttp-Sent-Millis: 1470987074283
OkHttp-Received-Millis: 1470987074422
You can do it in background Service. 您可以在后台服务中执行此操作。 And notify UI thread when get response through broadcast.
并通过广播获得响应时通知UI线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.