Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Erro ao enviar alunos criados offline

O app abre normalmente, mas se eu colocar modo avião e voltar para o normal eu recebo um erro:

08-01 18:14:36.922 10659-11265/br.com.alura.agenda D/OkHttp: --> POST http://192.168.1.6:8080/api/aluno http/1.1
08-01 18:14:36.922 10659-11265/br.com.alura.agenda D/OkHttp: Content-Type: application/json; charset=UTF-8
08-01 18:14:36.922 10659-11265/br.com.alura.agenda D/OkHttp: Content-Length: 160
08-01 18:14:36.922 10659-11265/br.com.alura.agenda D/OkHttp: {"caminhoFoto":null,"desativado":0,"endereco":"","id":"275850ab-a05c-4df4-9908-96eea93cdbfb","nome":"uhnhh","nota":0.0,"sincronizado":0,"site":"","telefone":""}
08-01 18:14:36.922 10659-11265/br.com.alura.agenda D/OkHttp: --> END POST (160-byte body)
08-01 18:14:36.927 10659-11265/br.com.alura.agenda D/OkHttp: <-- HTTP FAILED: java.net.ConnectException: Failed to connect to /192.168.1.6:8080
08-01 18:14:36.942 10659-10659/br.com.alura.agenda E/onFailure: requisicao falhou
08-01 18:14:36.942 10659-10726/br.com.alura.agenda V/FA: Recording user engagement, ms: 4038
08-01 18:14:36.947 10659-10726/br.com.alura.agenda D/FA: Logging event (FE): _e, Bundle[{_o=auto, _et=4038, _sc=FormularioActivity, _si=-3791547357770470309}]
08-01 18:14:36.972 10659-10726/br.com.alura.agenda V/FA: Activity paused, time: 16971857
08-01 18:14:36.982 10659-10726/br.com.alura.agenda V/FA: Activity resumed, time: 16971895
08-01 18:14:36.987 10659-10659/br.com.alura.agenda I/aluno sincronizado: 1
08-01 18:14:36.987 10659-10659/br.com.alura.agenda I/aluno sincronizado: 1
08-01 18:14:36.987 10659-10659/br.com.alura.agenda I/aluno sincronizado: 1
08-01 18:14:36.987 10659-10659/br.com.alura.agenda I/aluno sincronizado: 1
08-01 18:14:36.987 10659-10659/br.com.alura.agenda I/aluno sincronizado: 0
08-01 18:14:37.007 10659-10744/br.com.alura.agenda V/RenderScript: 0x560e4698 Launching thread(s), CPUs 4
08-01 18:14:37.367 10659-10659/br.com.alura.agenda I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3b6eb0ea time:16972287
08-01 18:14:41.982 10659-10726/br.com.alura.agenda V/FA: Inactivity, disconnecting from the service
08-01 18:14:54.157 10659-11692/br.com.alura.agenda D/OkHttp: --> GET http://192.168.1.6:8080/api/aluno/diff http/1.1
08-01 18:14:54.157 10659-11692/br.com.alura.agenda D/OkHttp: datahora: 2017-08-01T18:14:13.386
08-01 18:14:54.157 10659-11692/br.com.alura.agenda D/OkHttp: --> END GET
08-01 18:14:54.212 10659-11696/br.com.alura.agenda D/OkHttp: --> PUT http://192.168.1.6:8080/api/aluno/lista http/1.1
08-01 18:14:54.212 10659-11696/br.com.alura.agenda D/OkHttp: Content-Type: application/json; charset=UTF-8
08-01 18:14:54.212 10659-11696/br.com.alura.agenda D/OkHttp: Content-Length: 128
08-01 18:14:54.212 10659-11696/br.com.alura.agenda D/OkHttp: [{"caminhoFoto":null,"desativado":0,"endereco":"","id":null,"nome":"uhnhh","nota":0.0,"sincronizado":0,"site":"","telefone":""}]
08-01 18:14:54.212 10659-11696/br.com.alura.agenda D/OkHttp: --> END PUT (128-byte body)
08-01 18:14:55.872 10659-11692/br.com.alura.agenda D/OkHttp: <-- 200 OK http://192.168.1.6:8080/api/aluno/diff (1714ms)
08-01 18:14:55.872 10659-11692/br.com.alura.agenda D/OkHttp: Server: Apache-Coyote/1.1
08-01 18:14:55.872 10659-11692/br.com.alura.agenda D/OkHttp: Content-Type: application/json;charset=UTF-8
08-01 18:14:55.872 10659-11692/br.com.alura.agenda D/OkHttp: Transfer-Encoding: chunked
08-01 18:14:55.872 10659-11692/br.com.alura.agenda D/OkHttp: Date: Tue, 01 Aug 2017 21:14:48 GMT
08-01 18:14:55.872 10659-11696/br.com.alura.agenda D/OkHttp: <-- 400 Bad Request http://192.168.1.6:8080/api/aluno/lista (1661ms)
08-01 18:14:55.872 10659-11696/br.com.alura.agenda D/OkHttp: Server: Apache-Coyote/1.1
08-01 18:14:55.872 10659-11696/br.com.alura.agenda D/OkHttp: Content-Type: application/json;charset=UTF-8
08-01 18:14:55.872 10659-11696/br.com.alura.agenda D/OkHttp: Transfer-Encoding: chunked
08-01 18:14:55.872 10659-11696/br.com.alura.agenda D/OkHttp: Date: Tue, 01 Aug 2017 21:14:48 GMT
08-01 18:14:55.872 10659-11696/br.com.alura.agenda D/OkHttp: Connection: close
08-01 18:14:55.877 10659-11696/br.com.alura.agenda D/OkHttp: {"alunos":[]}
08-01 18:14:55.877 10659-11696/br.com.alura.agenda D/OkHttp: <-- END HTTP (13-byte body)
08-01 18:14:55.877 10659-11692/br.com.alura.agenda D/OkHttp: {"alunos":[],"momentoDaUltimaModificacao":"2017-08-01T18:14:48.359"}
08-01 18:14:55.877 10659-11692/br.com.alura.agenda D/OkHttp: <-- END HTTP (68-byte body)
08-01 18:14:55.902 10659-10659/br.com.alura.agenda D/AndroidRuntime: Shutting down VM
08-01 18:14:55.902 10659-10659/br.com.alura.agenda E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List br.com.alura.agenda.dao.AlunoSync.getAlunos()' on a null object reference
                                                                            at br.com.alura.agenda.sync.AlunoSincronizador$1.onResponse(AlunoSincronizador.java:70)
                                                                            at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:135)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5294)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
08-01 18:14:55.907 10659-10726/br.com.alura.agenda D/FA: Logging event (FE): _ae, Bundle[{_o=crash, _sc=ListaAlunosActivity, _si=-3791547357770470310, timestamp=1501622095906, fatal=1}]
08-01 18:14:55.922 10659-10726/br.com.alura.agenda V/FA: Using measurement service
08-01 18:14:55.922 10659-10726/br.com.alura.agenda V/FA: Connecting to remote service
08-01 18:14:56.167 10659-10659/br.com.alura.agenda E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: br.com.alura.agenda, PID: 10659
                                                                     java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List br.com.alura.agenda.dao.AlunoSync.getAlunos()' on a null object reference
                                                                         at br.com.alura.agenda.sync.AlunoSincronizador$1.onResponse(AlunoSincronizador.java:70)
                                                                         at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:135)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5294)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
08-01 18:14:56.177 10659-11744/br.com.alura.agenda I/FirebaseCrash: Sending crashes
08-01 18:14:57.697 10659-11744/br.com.alura.agenda I/FirebaseCrash: Response code: 200
08-01 18:14:57.702 10659-11744/br.com.alura.agenda I/FirebaseCrash: Report sent with crash report id: 0d29f4f388000000

O código:

public void sincronizaAlunosInternos(){
        final AlunoDAO dao = new AlunoDAO(context);

        List<Aluno> alunos = dao.listaNaoSincronizados();
        dao.close();
        Call<AlunoSync> call = new RetrofitInicializador()
                .getAlunoService().atualiza(alunos);

        call.enqueue(new Callback<AlunoSync>() {
            @Override
            public void onResponse(Call<AlunoSync> call, Response<AlunoSync> response) {
                AlunoSync alunoSync = response.body();
                AlunoDAO alunoDao = new AlunoDAO(context);
                alunoDao.sincroniza(alunoSync.getAlunos());
                alunoDao.close();
            }

            @Override
            public void onFailure(Call<AlunoSync> call, Throwable t) {

            }
        });
    }
public List<Aluno> listaNaoSincronizados() {
        SQLiteDatabase db = getReadableDatabase();
        String sql = "SELECT * FROM Alunos WHERE sincronizado = 0";
        Cursor cursor = db.rawQuery(sql, null);
        return populaAlunos(cursor);
    }
  @PUT("aluno/lista")
    Call<AlunoSync> atualiza(@Body List<Aluno> alunos);

Copiei o código inteiro apresentado das aulas para não ser erro ortográfico e fiz um teste e a busca do bando esta retornando um objeto. O que será que está errado?

1 resposta
solução!

Consegui resolver o problema.

Só agora que o deu erro em:

Antes:

  public void insere(Aluno aluno) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues dados = pegaDadosDoAluno(aluno);
        insereIdSeNecessario(aluno);
        db.insert("Alunos", null, dados);
    }

Depois(Correto):

  public void insere(Aluno aluno) {
        SQLiteDatabase db = getWritableDatabase();
        insereIdSeNecessario(aluno);
        ContentValues dados = pegaDadosDoAluno(aluno);
        db.insert("Alunos", null, dados);
    }