1

I got and SSLHandshakeException when I use synchronized connection to an HTTPS server. If I use asynchronized connection to same url I can't get two differentes answer:

Case 1: If I call the asynchronized request directly from main thread I got an 403 forbidden and empty response body.

Case 2: If I call the asynchronized request from a AsyncTask that had been called from main I got an 200 and the right response body that I am expecting.

So my question, why will the synchronized requested fail, and why I have to call the asynchronized request through an AsyncTask to get the expected answer?

UPDATE Looks like the problem with calling synchronous request inside the AsyncTask is that the Authenticator to OKHttp client is not done setting the Authenticator, therefor I am getting the Handshake Exception.

LAST UPDATE Got it to work. Refactored the posting string. From just getting the toString of a map to creating a JSONObject. Somehow, the authorization then passed and it wasn't even necessary to increased the timeout.

lagos
  • 1,968
  • 3
  • 17
  • 26

1 Answers1

2

As far as I understood, the problem is making a request form the main thread. If you read:
How to fix android.os.NetworkOnMainThreadException?.
You will understand that you cannot make network requests from the Ui thread(since Api 11 I think).
EDIT
Check google page:
http://developer.android.com/training/basics/network-ops/connecting.html.
EDIT 2
If the request is asynchronous, e.g using OKHttp async way, then the problem might come from the fact that you have a small timeout, I usually set it to 3-5 seconds(I had this problem).

Community
  • 1
  • 1
Andrei T
  • 2,985
  • 3
  • 21
  • 28
  • Network requests should be done on an async task, not main thread - it is really wrong. You can read on google docs. – Andrei T Oct 02 '15 at 08:46
  • I am not sure you know about asynchronous request provided by OKHTTP library. – lagos Oct 02 '15 at 08:49
  • Nice shot. Well, the asynchronous way worked without problem. But I want it to do synch way from inside a Async Task, then the timeout hint didn't work, even setting the timeout to 1 minute din't helped. – lagos Oct 02 '15 at 10:02
  • For me the timeout worked. I will update if another thing comes to mind. Do you use certs? – Andrei T Oct 02 '15 at 13:20
  • Yes, using certs, and setted to just one ciphering. Actually, I got it to work. Refactored the way of posting. – lagos Oct 04 '15 at 19:39