gpt4 book ai didi

java - Retrofit 需要太多时间才能得到响应

转载 作者:太空狗 更新时间:2023-10-29 14:38:53 27 4
gpt4 key购买 nike

我正在为 REST API 调用使用改造。使用 POSTMAN 客户端检查时,我可以在几分之一秒内得到响应。我用不到一秒钟的时间得到回应。当我使用 retrofit 调用请求时,它需要超过 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 GET200 OK 之间的时间差。同样,我也遇到了延迟,这类似于应用运行速度变慢。

最佳答案

在你的updateUI(upsDataResponse) 方法中从response fill recyclerview,listview 或任何其他字段一次填充

关于java - Retrofit 需要太多时间才能得到响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51416333/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com