WebClient.java:
package br.com.potatotech.agenda.webclient;
import android.util.Log;
import java.io.IOException;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
/**
* Created by Allan on 18/09/2016.
* webclient
*/
public class WebClient {
public static String post(String json){
try {
URL url = new URL("https://caelum.com.br/mobile");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
PrintStream output = new PrintStream(connection.getOutputStream());
output.println(json);
Log.i("json", json);
connection.connect();
Scanner scanner = new Scanner((Readable) connection.getOutputStream());
String resposta = scanner.next();
Log.d("json", resposta);
return resposta;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Se eu copio e colo o código de resposta, o WebClient funciona normalmente. Porêm, o meu mostra esse erro no logcat:
09-18 16:19:48.052 24363-24939/br.com.potatotech.agenda W/System.err: javax.net.ssl.SSLHandshakeException: Connection closed by peer
09-18 16:19:48.091 24363-24939/br.com.potatotech.agenda W/System.err: at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
09-18 16:19:48.092 24363-24939/br.com.potatotech.agenda W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:324)
09-18 16:19:48.092 24363-24939/br.com.potatotech.agenda W/System.err: at com.android.okhttp.internal.http.SocketConnector.connectTls(SocketConnector.java:103)
09-18 16:19:48.092 24363-24939/br.com.potatotech.agenda W/System.err: at com.android.okhttp.Connection.connect(Connection.java:143)
09-18 16:19:48.093 24363-24939/br.com.potatotech.agenda W/System.err: at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)
...(omitido)...
09-18 16:19:48.093 24363-24939/br.com.potatotech.agenda W/System.err: at br.com.potatotech.agenda.webclient.WebClient.post(WebClient.java:29)
09-18 16:19:48.093 24363-24939/br.com.potatotech.agenda W/System.err: at br.com.potatotech.agenda.webclient.PostInBackground.doInBackground(PostInBackground.java:23)
09-18 16:19:48.093 24363-24939/br.com.potatotech.agenda W/System.err: at br.com.potatotech.agenda.webclient.PostInBackground.doInBackground(PostInBackground.java:14)
O que pode estar causando isso? Thanks.