尝试从asp.net web api调用获取响应时android studio中的超时异常

时间:2017-09-18 06:15:16

标签: c# android asp.net rest asp.net-web-api

我正在尝试在android中使用一个asp.net web api,需要大约17500毫秒才能响应(按照邮差)。它在android studio中给我一个超时错误。 但是当我尝试使用邮递员明确地运行api时,我得到了理想的结果。

以下是摘录:

java.net.URL url = new java.net.URL(vehicleurl);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
conn.setConnectTimeout(15000000);


Request req = new Request.Builder()
                        .url(vehicleurl)
                        .build();
Response response = client.newCall(req).execute();

1 个答案:

答案 0 :(得分:0)

试试这个。

manifest

<uses-permission android:name="android.permission.INTERNET"/>

修改

HttpURLConnection类

 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
 // edit here ,change time
 connection.setConnectTimeout(20000);
 connection.setReadTimeout(20000);

OKHttp

private final OkHttpClient client;

public ConfigureTimeouts() throws Exception {
client = new OkHttpClient.Builder()
    .connectTimeout(100, TimeUnit.SECONDS)
    .writeTimeout(100, TimeUnit.SECONDS)
    .readTimeout(100, TimeUnit.SECONDS)
    .build();
}