在广播接收器中使用okhttp android时出现SocketTimeOutException

时间:2016-06-14 11:52:26

标签: android sockets okhttp3 socket-timeout-exception

我需要每3分钟向服务器发送一次数据。我正在使用警报管理器每3分钟触发一次,我在广播接收器中使用asynctask但是我在okhttp中收到SocketTimeoutException错误。怎么解决这个问题。我检查了okhttp中的方法,但我找不到方法mOkHttpClient.setConnectTimeout(REQ_TIMEOUT,TimeUnit.SECONDS);

这是代码

 public class AlarmReceiver extends BroadcastReceiver {
private Context context;
private VALERTApplication mValertApplication;

@Override
public void onReceive(Context context, Intent intent) {
    this.context=context;
    new DownloadTask()
                .execute();
}

  private class DownloadTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        // do your request in here so that you don't interrupt the UI thread
        try {
            RequestBody formBody = new FormBody.Builder()
                    .add("userUUID", "281FDACE- 6400-4091-8BD1-F6CC2E8F64AB")
                    .add("readingTime", "182525")
                    .add("readingdate", "20160612")
                    .add("readingValue", "12")
                    .add("timezone", "EST")
                    .add("readingType", "9")
                    .build();

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("https://apps.vocare.com/api/index.cfm?endpoint=/readings")
                    .post(formBody)
                    .build();

            Response response = client.newCall(request).execute();
            return response.body().string();

        } catch (IOException e) {
            return "Unable to retrieve data. URL may be invalid.";
        }
    }
    @Override
    protected void onPostExecute(String result) {
            Log.e("result",result);
        }
    }

}

我收到错误

  java.net.SocketTimeoutException: SSL handshake timed out
  at   com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
  at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:318)
  at   okhttp3.internal.io.RealConnection.connectTls(RealConnection.java:239)
  at okhttp3.internal.io.RealConnection.establishProtocol(RealConnection.java:196)
  at okhttp3.internal.io.RealConnection.buildConnection(RealConnection.java:171)
  at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
  at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:187)
  at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123)
  at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93)
  at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
  at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
  at okhttp3.RealCall.getResponse(RealCall.java:243)
  at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
  at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
  at okhttp3.RealCall.execute(RealCall.java:57)
  at com.vcare.services.AlarmReceiver$DownloadTask.doInBackground(AlarmReceiver.java:76)
  at com.vcare.services.AlarmReceiver$DownloadTask.doInBackground(AlarmReceiver.java:48)
  at android.os.AsyncTask$2.call(AsyncTask.java:292)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  at java.lang.Thread.run(Thread.java:818)
  06-14 16:48:44.259 13881-13881/com.vocare E/result: Unable to retrieve data. URL may be invalid.

0 个答案:

没有答案