我正在对REST API调用进行改造。使用 POSTMAN客户端进行检查时,响应可以在几秒钟内得到。我花了不到一秒钟的时间来回应。当我使用翻新程序调用请求时,它需要5到10秒钟以上的时间才能获得响应。它显示我的应用程序运行缓慢。请给我一个解决方案。
注意:在.NET中创建的Web API
下面是我的代码
//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();
}
我的请求电话如下:
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();
}
});
}
还添加了以下日志:
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)
在上面的日志中,看到 End GET 和 200 OK 之间的时差。同样,我正在延迟,它类似于应用程序的运行速度较慢。
答案 0 :(得分:0)
从响应填充recyclerview,listview或其他任何字段中立即填充您的updateUI(upsDataResponse)方法