Estou recebendo a seguinte exceção:
W/System.err: javax.net.ssl.SSLHandshakeException: SSL handshake aborted: ssl=0xa2338400: I/O error during system call, Connection reset by peer
W/System.err: at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:318)
at com.android.okhttp.Connection.upgradeToTls(Connection.java:201)
at com.android.okhttp.Connection.connect(Connection.java:155)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:217)
at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
at alura.com.agenda2.WebClient.post(WebClient.java:26)
at alura.com.agenda2.EnviaAlunosTask.doInBackground(EnviaAlunosTask.java:35)
at alura.com.agenda2.EnviaAlunosTask.doInBackground(EnviaAlunosTask.java:18)
W/System.err: 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)
Código WebClient.java:
import java.io.IOException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import javax.net.ssl.HttpsURLConnection;
public class WebClient {
public String post(String json){
try {
URL url = new URL("https://www.caelum.com.br/mobile");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestProperty("Content-type","application/json");
connection.setRequestProperty("Accept","application/json");
connection.setDoOutput(true);
connection.setDoInput(true);
PrintStream output = new PrintStream(connection.getOutputStream());
output.println(json);
connection.connect();
Scanner scanner = new Scanner(connection.getInputStream());
String resposta = scanner.next();
return resposta;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Código AlunoConverter.java:
import org.json.JSONException;
import org.json.JSONStringer;
import java.util.List;
import alura.com.agenda2.modelo.Aluno;
public class AlunoConverter {
public String converteParaJSON(List<Aluno> alunos) {
JSONStringer js = new JSONStringer();
try {
js.object().key("list").array().object().key("aluno").array();
for (Aluno aluno: alunos) {
js.object().key("nome").value(aluno.getNome()).key("nota").value(aluno.getNota());
js.endObject();
}
js.endArray().endObject().endArray().endObject();
} catch (JSONException e) {
e.printStackTrace();
}
return js.toString();
}
}
Em EnviaAlunosTask.java:
@Override
protected String doInBackground(Void... objects) {
AlunoDAO dao = new AlunoDAO(context);
List<Aluno> alunos = dao.buscaAlunos();
dao.close();
AlunoConverter conversor = new AlunoConverter();
String json = conversor.converteParaJSON(alunos);
WebClient webClient = new WebClient();
String resposta = webClient.post(json);
return resposta;
}
Será que o WebService ainda está no ar?