[英]Retrofit takes too much time to get response
I'm using retrofit for REST API call. 我正在使用REST API调用改造。 When checking using POSTMAN client the response i can get it in fraction of seconds . 使用POSTMAN客户端进行检查时, 我可以在几秒钟内得到响应 。 I takes less than a second to get response. 我花了不到一秒的时间才得到回应。 While i called the request using the retrofit it takes more than 5 to 10 seconds to get the response . 虽然我使用改造调用了请求,但需要5到10秒才能获得响应 。 It shows my app working slower. 它显示我的应用程序工作较慢。 Kindly suggest me a solution. 请建议我一个解决方案。
Note: Web API created in .NET 注意:在.NET中创建的Web API
Below is my code 以下是我的代码
//Retrofit client code:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new
OkHttpClient.Builder().addInterceptor(interceptor)
.connectTimeout(600, TimeUnit.SECONDS)
.readTimeout(600, TimeUnit.SECONDS)
.build();
if (client != null) {
ConnectionPool pool = client.connectionPool();
if (pool != null) {
pool.evictAll();
}
}
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
And my request call is below: 我的请求电话如下:
private void makeServiceCallUPSData(String upsNameId, String spvId) {
showLoadingView();
if (calledFrom.equals(Constants.UPS_EDIT)) {
logId = upsId;
} else {
logId = "0";
}
apiService.getUPSData(upsNameId, spvId, logId, authorizationToken).enqueue(new Callback<UPSDataResponse>() {
@Override
public void onResponse(Call<UPSDataResponse> call, Response<UPSDataResponse> response) {
if (response.isSuccessful()) {
UPSDataResponse upsDataResponse = response.body();
hideLoadingView();
if (upsDataResponse.getCode() == Constants.RESPONSE_SUCCESS) {
updateUI(upsDataResponse);
} else {
showErrorAlertDialog(upsDataResponse.getMessage());
}
Logger.infoLog("Logg resp " + response.body());
}
}
@Override
public void onFailure(Call<UPSDataResponse> call, Throwable t) {
//dismissProgressDialog();
hideLoadingView();
//Toast.makeText(thisActivity, "Server not Responsding. Try after Sometime", Toast.LENGTH_SHORT).show();
}
});
}
Also added the below the log: 还在日志下面添加了:
07-19 16:57:07.920 32345-2045/com.vcarel.test D/OkHttp: --> GET http://14.143.142.43/api/HMS/HMSUPS/GetUPSData?upsNameId=2370&spvId=6&logId=0
07-19 16:57:07.920 32345-2045/com.vcarel.test D/OkHttp: Cache-Control: max-age=640000
07-19 16:57:07.920 32345-2045/com.vcarel.test D/OkHttp: Authorization: bearer 6ndJHgICCPj6zaMpW6SzEZN06zcDHE5PaEHVJ9jol--Md5Hla_J-Nx9-wynQ_nG8OBjNn5LjA5
07-19 16:57:07.920 32345-2045/com.vcarel.test D/OkHttp: --> END GET
07-19 16:57:12.670 32345-2045/com.vcarel.test D/OkHttp: <-- 200 OK http://14.143.142.43/api/HMS/HMSUPS/GetUPSData?upsNameId=2370&spvId=6&logId=0 (4747ms)
07-19 16:57:12.670 32345-2045/com.vcarel.test D/OkHttp: Cache-Control: no-cache
07-19 16:57:12.670 32345-2045/com.vcarel.test D/OkHttp: Pragma: no-cache
07-19 16:57:12.670 32345-2045/com.vcarel.test D/OkHttp: Content-Type: application/json; charset=utf-8
07-19 16:57:12.670 32345-2045/com.vcarel.test D/OkHttp: Expires: -1
07-19 16:57:12.670 32345-2045/com.vcarel.test D/OkHttp: Server: Microsoft-IIS/8.5
07-19 16:57:12.670 32345-2045/com.vcarel.test D/OkHttp: X-AspNet-Version: 4.0.30319
07-19 16:57:12.670 32345-2045/com.vcarel.test D/OkHttp: X-Powered-By: ASP.NET
07-19 16:57:12.670 32345-2045/com.vcarel.test D/OkHttp: Date: Thu, 19 Jul 2018 11:27:23 GMT
07-19 16:57:12.670 32345-2045/com.vcarel.test D/OkHttp: Content-Length: 513
07-19 16:57:12.670 32345-2045/com.vcarel.test D/OkHttp: {"Code":200,"Message":"Success","Respone":{"MainCurrentB":"1.000","MainCurrentR":"1.000","MainCurrentY":"1.000","MainVoltageB":"1.000","MainVoltageR":"1.000","MainVoltageY":"1.000","MainRectifierVoltage":"2.500","BypassVoltage":"1.000","BypassCurrent":"2.500","BypassFr48to52Hz":"0.000","OutputVoltage":"2.225","OutputCurrent":"2.500","OutputFr48to52Hz":"0.000","OutputTem":"2.500","BatteryVdc":"25.000","BatteryLb":"0.000","DateTime":"2018-07-19 16:42:00","Remarks":"test","fromVDC":"300.000","toVDC":"350.000"}}
07-19 16:57:12.670 32345-2045/com.vcarel.test D/OkHttp: <-- END HTTP (513-byte body)
In the above log see the time difference between End GET and 200 OK Likewise i'm getting delay it resembles app being working slower. 在上面的日志中看到结束GET和200之间的时差确定同样我得到延迟它类似于应用程序工作较慢。
来自响应填充recyclelerview,listview或任何其他更多fiels在updateUI(upsDataResponse)方法中立即填写
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.