Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Android II, erro após a implementação do código javax.net.ssl.SSLProtocolException

Olá, após a implementação do código na ultima aula tive essa Exception, deve-se implementar algo a mais?

Segue a seguir o que o Android Studio me enviou após enviar os dados do aluno.

W/zygote: Verification of java.security.cert.Certificate[] com.android.tools.profiler.support.network.httpurl.HttpsURLConnection$.getLocalCertificates() took 3.686s
W/zygote: Verification of void com.android.tools.profiler.support.network.httpurl.HttpsURLConnection$.setUseCaches(boolean) took 348.538ms
W/zygote: Verification of java.lang.String com.android.tools.profiler.support.network.httpurl.TrackedHttpURLConnection.getHeaderFieldKey(int) took 101.302ms
W/System.err: javax.net.ssl.SSLProtocolException: Read error: ssl=0x96d184c0: Failure in SSL library, usually a protocol error
W/System.err: error:100000d7:SSL routines:OPENSSL_internal:SSL_HANDSHAKE_FAILURE (external/boringssl/src/ssl/ssl_lib.cc:717 0xa7830dc3:0x00000000)
W/System.err:     at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
W/System.err:     at com.android.org.conscrypt.SslWrapper.read(SslWrapper.java:384)
W/System.err:     at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:541)
W/System.err:     at com.android.okhttp.okio.Okio$2.read(Okio.java:136)
W/System.err:     at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
W/System.err:     at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
W/System.err:     at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
W/System.err:     at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
W/System.err:     at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
W/System.err:     at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
W/System.err:     at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err:     at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(Unknown Source:0)
W/System.err:     at com.android.tools.profiler.support.network.httpurl.TrackedHttpURLConnection.getInputStream(TrackedHttpURLConnection.java:356)
W/System.err:     at com.android.tools.profiler.support.network.httpurl.HttpsURLConnection$.getInputStream(HttpsURLConnection$.java:261)
W/System.err:     at br.com.example.admin.agenda.WebClient.post(WebClient.java:25)
W/System.err:     at br.com.example.admin.agenda.EnviaAlunosTask.doInBackground(EnviaAlunosTask.java:37)
W/System.err:     at br.com.example.admin.agenda.EnviaAlunosTask.doInBackground(EnviaAlunosTask.java:14)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
W/System.err:     at java.lang.Thread.run(Thread.java:764)
I/System.out: Estou retornando resposta como: null

A seguir o meu código na Classe WebClient, o erro se dá quando eu crio uma instância da classe Scanner..

3 respostas
public class WebClient {
    public String post(String json){//Post para processar dados no servidor e get para enviar(Dados ja preparados para o servidor)
        try {
            URL url = new URL("https://www.caelum.com.br/mobile");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Content-type", "application/json");
            connection.setRequestProperty("Accept", "application/json");

            connection.setDoOutput(true);//quero fazer POST

            PrintStream output = new PrintStream(connection.getOutputStream());
            output.println(json);
            connection.connect();

            //pegar resposta
            Scanner scanner = new Scanner(connection.getInputStream());
            String resposta = scanner.next();

            return resposta;

        } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    return null;
    }
}
solução!

Oi Lucas, tudo bem ?

Acho que faltou você falar que quer ser um post.

Basta adicionar essa linha :

            connection.setRequestMethod("POST");

Olá Matheus, resolveu muito obrigado!

I/System.out: Estou retornando resposta como: {"media":"8","quantidade":1}

o teste acima foi feito com apenas um aluno com 4 estrelas. Um forte abraço!