相关疑难解决方法(0)

Retrofit中的SocketTimeoutException

我试图向服务器POST请求获取数据,但有时它发生了SocketTimeoutException!

我曾经Ok3Client解决它,但我面临同样的例外如何解决它?

我的代码如下

public void getNormalLogin() {
        if (mProgressDialog == null) {
            mProgressDialog = ViewUtils.createProgressDialog(mActivity);
            mProgressDialog.show();
        } else {
            mProgressDialog.show();
        }

        if (Build.VERSION.SDK != null && Build.VERSION.SDK_INT > 13) {
            restadapter = new RestAdapter.Builder().setEndpoint(HOST).setLogLevel(RestAdapter.LogLevel.FULL).setClient(new Ok3Client(new OkHttpClient())).build();
            mApi = restadapter.create(Api.class);
            mApi.SignIn(etEmail.getText().toString(), etPassword.getText().toString(), new Callback<ArrayList<SignUpMainBean>>() {
                @Override
                public void success(ArrayList<SignUpMainBean> signUpMainBeen, Response response) {
                    mProgressDialog.dismiss();
                    LOGD("Status:: ::", String.valueOf(response.getStatus()));
                    LOGD("Code:: ::", String.valueOf(signUpMainBeen.get(0).getCode()));
                    if (signUpMainBeen != null && signUpMainBeen.size() > 0) {
                        if (signUpMainBeen.get(0).getCode() == 1) {

                            for …
Run Code Online (Sandbox Code Playgroud)

android socket-timeout-exception retrofit okhttp

35
推荐指数
3
解决办法
4万
查看次数

http连接超时问题

当我尝试使用连接到网址的HttpClient时,我遇到了一个问题.即使我设置了连接超时后,http连接也需要更长的时间才能超时.

int timeoutConnection = 5000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);

int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
Run Code Online (Sandbox Code Playgroud)

它大部分时间都很完美.然而,每隔一段时间,http连接就会永远运行并忽略setconnectiontimeout,特别是当手机连接到wifi时,手机处于空闲状态.

因此,在手机闲置后,我第一次尝试连接时,http连接忽略了setconnectiontimeout并且永远运行,在我取消它并再次尝试之后,它每次都像魅力一样.但是有一次它不起作用会产生threadtimeout错误,我尝试使用不同的线程,它可以工作,但我知道线程运行了很长时间.

我明白wifi在闲置时睡觉,但我不明白为什么它忽略了setconnectiontimeout.

任何人都可以帮忙,我真的很感激.

java android timeout apache-httpclient-4.x

17
推荐指数
2
解决办法
3万
查看次数

应用程序崩溃 java.net.SocketTimeoutException: timeout (Kotlin, Retrofit)

如果没有响应,应用程序会在这条线上崩溃。

chain.proceed(requestBuilder.build())
Run Code Online (Sandbox Code Playgroud)

这是我的 RetrofitClient 课程

class RetrofitClient {

    private val apiService: ApiServiceInterface

    init {
        val builder = Retrofit.Builder()
        builder.baseUrl(RequestParameters.BASE_URL)
        builder.addConverterFactory(GsonConverterFactory.create())
        builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//added for adapter
        builder.client(getClient())
        val retrofit = builder.build()
        apiService = retrofit.create(ApiServiceInterface::class.java)
    }

    companion object {
        private var clientInstance: RetrofitClient? = null
        lateinit var context: Context

        fun getInstance(): RetrofitClient {
            if (clientInstance == null) {
                clientInstance = RetrofitClient()
            }
            return clientInstance as RetrofitClient
        }
    }

    fun getApiInterface(): ApiServiceInterface {
        return apiService
    }

    private fun getClient(): OkHttpClient {

        val httpClient = OkHttpClient.Builder() …
Run Code Online (Sandbox Code Playgroud)

android httpclient kotlin rx-java retrofit2

8
推荐指数
1
解决办法
3407
查看次数

为什么我用OkHttp收到java.net.SocketTimeoutException?

这是我的配置:

httpClient = new OkHttpClient.Builder()
        .callTimeout(Duration.ofSeconds(60))
        .connectTimeout(Duration.ofSeconds(60))
        .readTimeout(Duration.ofSeconds(60))
        .writeTimeout(Duration.ofSeconds(60))
        .build();
Run Code Online (Sandbox Code Playgroud)

我有一个使用此客户端的多线程进程。运行几秒钟后,我得到:

java.net.SocketTimeoutException:
在okio.Okio $ 4.newTimeoutException(Okio.java:232)
在okio.AsyncTimeout.exit(AsyncTimeout.java:286)
在okio.AsyncTimeout $ 2.read(AsyncTimeout.java:241)
在.RealBufferedSource.indexOf(RealBufferedSource.java:358)

如果将超时配置为60秒怎么办?

编辑:
即使添加自定义调度程序也无济于事:

Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(Integer.MAX_VALUE);
dispatcher.setMaxRequestsPerHost(Integer.MAX_VALUE);
Run Code Online (Sandbox Code Playgroud)

技术细节:
与我所说的相反,我在Linux机器上同时运行客户端和服务器:

客户端计算机:net.ipv4.tcp_keepalive_time = 7200
服务器计算机:net.ipv4.tcp_keepalive_time = 7200

java http okhttp3

6
推荐指数
1
解决办法
383
查看次数

HttpURLConnection抛出java.net.SocketTimeoutException:在Android 4.1.1中SSL握手超时

在Android 5.0及更高版本中运行时,我的代码运行正常.但是在Android 4.1.1中它会抛出java.net.SocketTimeoutException:SSL握手超时.

    URL url;
    HttpURLConnection connection = null; 
    String charset = "UTF-8";

    String accessToken = "";

    try {

        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("client_id", CLIENT_ID));
        postParameters.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
        postParameters.add(new BasicNameValuePair("grant_type", CLIENT_CREDENTIALS_GRANT_TYPE));

        //Create connection
        url = new URL(ACCESS_TOKEN_URL);
        connection = (HttpURLConnection)url.openConnection();
        connection.setReadTimeout( 10000 /*milliseconds*/ );
        connection.setConnectTimeout( 15000 /* milliseconds */ );
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.write(getQuery(postParameters).getBytes(charset));
        wr.flush();
        wr.close();

        int responseCode = connection.getResponseCode();
        InputStream is = null;

        if(responseCode==HttpStatus.SC_OK) …
Run Code Online (Sandbox Code Playgroud)

java ssl android timeout httpurlconnection

5
推荐指数
0
解决办法
8538
查看次数

java.net.SocketTimeoutException(ANDROID)

我用以下代码连接 -

URL url = new URL("https://results.bput.ac.in/");

HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(1000 * 20);

urlc.connect();
Run Code Online (Sandbox Code Playgroud)

它返回了一个SocketTimeoutException.

例外

我得到的确切例外是

java.net.SocketTimeoutException: failed to connect to results.bput.ac.in/14.139.212.166 (port 443) after 90000ms
Run Code Online (Sandbox Code Playgroud)

有时这个 -

java.net.SocketTimeoutException: failed to connect to results.bput.ac.in/14.139.212.166 (port 80) after 90000ms
Run Code Online (Sandbox Code Playgroud)
  • 尝试删除urlc.setConnectTimeout(1000 * 20);,仍然得到了例外.
  • http而不是https 检查URL url = new URL("http://results.bput.ac.in/");但没有结果.
  • 与之相关URL url = new URL("https://www.facebook.com/");并获得成功回应.
  • 检查更改超时时间但同样的异常.

问题在于这个特定的网址 - http://results.bput.ac.in/.

信息

我给出的这个链接http://results.bput.ac.in/完全适用于任何网络浏览器,没有任何滞后.

我得到的信息是,一些人不能打开这个网站,它的滞后但我可以毫不拖延地打开它.

我的研究

我已经尝试过这个问题,这个问题, …

java android socket-timeout-exception

5
推荐指数
1
解决办法
3871
查看次数