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

No meu projeto não aparece os alunos no emulador, eu já terminei aula

Bom dia,

Aqui esta o que o meu console esta falando sobre este erro:

08-24 17:26:06.496 19589-19589/? W/nAnnotationIntrospector: Unable to load JDK7 annotation types; will have to skip
08-24 17:26:06.519 19589-19589/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default
08-24 17:26:06.525 19589-19589/? W/System.err: WARNING: could not load Java7 Path class
08-24 17:26:06.543 19589-19611/? D/OkHttp: --> GET http://192.168.1.4:8080/api/aluno http/1.1
08-24 17:26:06.543 19589-19611/? D/OkHttp: --> END GET
08-24 17:26:06.550 1327-1360/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property

                                                 [ 08-24 17:26:06.552 19589:19589 D/         ]
                                                 HostConnection::get() New Host Connection established 0xa3c96bc0, tid 19589
08-24 17:26:06.598 1327-2364/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer

                                                 [ 08-24 17:26:06.598 19589:19613 D/         ]
                                                 HostConnection::get() New Host Connection established 0xa3c96f80, tid 19613
08-24 17:26:06.599 19589-19613/? I/OpenGLRenderer: Initialized EGL, version 1.4
08-24 17:26:06.599 19589-19613/? D/OpenGLRenderer: Swap behavior 1
08-24 17:26:06.600 19589-19613/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-24 17:26:06.600 19589-19613/? D/OpenGLRenderer: Swap behavior 0
08-24 17:26:06.617 1327-2364/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
08-24 17:26:06.630 1327-2364/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
08-24 17:26:06.718 19589-19589/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
08-24 17:26:06.858 1621-1644/system_process I/ActivityManager: Displayed com.example.guilherme.agentaaluno/.ListaAlunosActivity: +631ms
08-24 17:26:07.043 2599-11264/? I/Icing: Indexing 3C89E3DC5DB25541AAC4086F59404DB5B48B73E8 from com.google.android.gms
08-24 17:26:07.059 1621-1644/system_process D/gralloc_ranchu: gralloc_unregister_buffer: exiting HostConnection (is buffer-handling thread)
08-24 17:26:07.062 2599-11264/? I/Icing: Indexing done 3C89E3DC5DB25541AAC4086F59404DB5B48B73E8
08-24 17:26:07.062 2599-11264/? I/Icing: Indexing 27CD3440A8C13F270BBB25A25D339269BBBA475A from com.google.android.googlequicksearchbox
08-24 17:26:07.079 2599-11264/? I/Icing: Indexing done 27CD3440A8C13F270BBB25A25D339269BBBA475A
08-24 17:26:07.080 2599-11264/? I/Icing: Indexing 3C89E3DC5DB25541AAC4086F59404DB5B48B73E8 from com.google.android.gms
08-24 17:26:07.080 2599-11264/? I/Icing: Indexing done 3C89E3DC5DB25541AAC4086F59404DB5B48B73E8
08-24 17:26:07.141 1327-1327/? W/SurfaceFlinger: couldn't log to binary event log: overflow.
08-24 17:26:07.316 19589-19611/com.example.guilherme.agentaaluno D/OkHttp: <-- 200 OK http://192.168.1.4:8080/api/aluno (772ms)
08-24 17:26:07.316 19589-19611/com.example.guilherme.agentaaluno D/OkHttp: Server: Apache-Coyote/1.1
08-24 17:26:07.316 19589-19611/com.example.guilherme.agentaaluno D/OkHttp: Content-Type: application/json;charset=UTF-8
08-24 17:26:07.316 19589-19611/com.example.guilherme.agentaaluno D/OkHttp: Transfer-Encoding: chunked
08-24 17:26:07.316 19589-19611/com.example.guilherme.agentaaluno D/OkHttp: Date: Thu, 24 Aug 2017 21:26:07 GMT
08-24 17:26:07.316 19589-19611/com.example.guilherme.agentaaluno D/OkHttp: {"alunos":[{"id":"25068df5-6a21-4998-9a0a-5e33297fdea7","nome":"Paulo","endereco":"rua 3","telefone":"32145678","site":"www.site.com","nota":10.0,"desativado":0,"idCliente":5},{"id":"89d7b14d-099a-453e-bf21-173fb279ef6a","nome":"test","endereco":"rua 2","telefone":"12345678","site":"www.site.com","nota":9.0,"desativado":0,"idCliente":4},{"id":"8bd41cd2-0919-4296-93dd-6f2f2ff52a9f","nome":"guiab","endereco":"rua 5","telefone":"56781234","site":"www.google.com","nota":10.0,"desativado":0,"idCliente":1},{"id":"91ca4f7a-3d4b-48f1-90de-391510026d68","nome":"suci","endereco":"rua dog","telefone":"445648465","site":"www.google.com","nota":8.0,"desativado":0,"idCliente":0},{"id":"bb966942-27bc-4869-8331-3e655abe7989","nome":"guiar","endereco":"t.sa","telefone":"4513252","site":"www.got.com","nota":5.0,"desativado":0,"idCliente":0},{"id":"cfb81c77-c169-48c3-8d5a-565f9b2dcd23","nome":"guilherme","endereco":"rua 9","telefone":"98563214","site":"www.site.com","nota":9.0,"desativado":0,"idCliente":0}],"momentoDaUltimaModificacao":"2017-08-24T17:26:07.864"}
08-24 17:26:07.316 19589-19611/com.example.guilherme.agentaaluno D/OkHttp: <-- END HTTP (1059-byte body)
08-24 17:26:07.321 19589-19589/com.example.guilherme.agentaaluno E/onFailure chamado: Problem deserializing 'setterless' property 'alunos': get method returned null
                                                                                          at [Source: okhttp3.ResponseBody$BomAwareReader@796988c; line: 1, column: 11] (through reference chain: com.example.guilherme.agentaaluno.dto.AlunoSync["alunos"])
08-24 17:26:12.451 2599-2608/? W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/metrics.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
08-24 17:26:12.453 2599-2608/? W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/help_responses.db.18' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
08-24 17:26:12.455 2599-2608/? W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/auto_complete_suggestions.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

O erro esta aqui -->

08-24 17:26:06.550 1327-1360/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property

                                                 [ 08-24 17:26:06.552 19589:19589 D/         ]
                                                 HostConnection::get() New Host Connection established 0xa3c96bc0, tid 19589

Vou também compartilhar codigo AlunoSync:

package com.example.guilherme.agentaaluno.dto;

import com.example.guilherme.agentaaluno.modelo.Aluno;

import java.util.List;

/**
 * Created by guilherme on 22/08/17.
 */

public class AlunoSync {

    private List<Aluno> aluno;
    private String momentoDaUltimaModificacao;

    public String getMomentoDaUltimaModificacao() {
        return momentoDaUltimaModificacao;
    }

    public List<Aluno> getAlunos() {
        return aluno;
    }

}

Alguém poderia me ajudar como resolver este problema?

7 respostas

Oi Gui, blz?

Pelo o que indica no erro, acredito que apenas mudando o nome do atributo private List<Aluno> aluno; para alunos do AlunoSync já resolve :)

[]s

Bom dia,

Estou enviando o codigo que eu fiz na aula:

public class AlunoSync {

    private List<Aluno> AlunoSync;
    private String momentoDaUltimaModificacao;

    public String getMomentoDaUltimaModificacao() {
        return momentoDaUltimaModificacao;
    }

    public List<Aluno> getAlunos() {
        return AlunoSync;
    }

}
public interface AlunoService {

    @POST("aluno")
    Call<Void> insere(@Body Aluno aluno);

    @GET("aluno")
    Call<AlunoSync> lista();
}
private void carregaLista() {
        AlunoDAO dao = new AlunoDAO(this);
        List<Aluno> alunos = dao.buscaAluno();

        for (Aluno aluno : alunos){
            Log.i("id do aluno", String.valueOf(aluno.getId()));
        }

        dao.close();

        AlunosAdapter adapter = new AlunosAdapter(this, alunos);
        listaAlunos.setAdapter(adapter);
    }

    protected void onResume() {
        super.onResume();

        Call<AlunoSync> call = new RetrofitInicializador().getAlunoService().lista();
        call.enqueue(new Callback<AlunoSync>() {
            @Override
            public void onResponse(Call<AlunoSync> call, Response<AlunoSync> response) {
                AlunoSync alunosSync = response.body();
                AlunoDAO dao = new AlunoDAO(ListaAlunosActivity.this);
                dao.sincroniza(alunosSync.getAlunos());

                dao.close();
                carregaLista();
            }

            @Override
            public void onFailure(Call<AlunoSync> call, Throwable t) {
                Log.e("onFailure chamado", t.getMessage());
            }
        });

        carregaLista();
    }
@JsonIgnoreProperties(ignoreUnknown = true)
public class Aluno implements Serializable {

    private String id;
    private String nome;
    private String endereco;
    private String telefone;
    private String site;
    private Double nota;
    private String caminhoFoto;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    public String getSite() {
        return site;
    }

    public void setSite(String site) {
        this.site = site;
    }

    public Double getNota() {
        return nota;
    }

    public void setNota(Double nota) {
        this.nota = nota;
    }

    public String getCaminhoFoto() {
        return caminhoFoto;
    }

    public void setCaminhoFoto(String caminhoFoto) {
        this.caminhoFoto = caminhoFoto;
    }

    @Override
    public String toString() {
        return getId() + " - " + getNome();
    }

}

também estou enviando codigo AlunoDAO:

private boolean existe(Aluno alunos) {

        SQLiteDatabase db = getReadableDatabase();
        String existe = "SELECT id FROM Aluno WHERE id=? LIMIT 1";
        Cursor cursor = db.rawQuery(existe, new String[]{alunos.getId()});
        int quantidade = cursor.getCount();
        return quantidade > 0;
    }

    public void sincroniza(List<Aluno> aluno) {

        for (Aluno alunos: aluno){

            if(existe(alunos)){
                altera(alunos);
            }else{
                insere(alunos);
            }

        }

    }

}

Oi Guilherme, blz?

Veja que na implementação que você fez do AlunoSync você está usando agora o atributo como alunoSync sendo que você precisa colocar de acordo com o que vem no JSON, ou seja, alunos. No código ficaria da seguinte maneira:

public class AlunoSync {

    private List<Aluno> alunos;
    private String momentoDaUltimaModificacao;

    public List<Aluno> getAlunos() {
        return alunos;
    }

    public void setAlunos(List<Aluno> alunos) {
        this.alunos = alunos;
    }

    public String getMomentoDaUltimaModificacao() {
        return momentoDaUltimaModificacao;
    }

    public void setMomentoDaUltimaModificacao(String momentoDaUltimaModificacao) {
        this.momentoDaUltimaModificacao = momentoDaUltimaModificacao;
    }
}

Esta dando outro erro no console:

08-25 17:41:42.222 18906-18906/com.example.guilherme.agentaaluno E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.example.guilherme.agentaaluno, PID: 18906
                                                                                   java.lang.RuntimeException: Unable to resume activity {com.example.guilherme.agentaaluno/com.example.guilherme.agentaaluno.ListaAlunosActivity}: android.database.sqlite.SQLiteException: no such table: alunos (code 1): , while compiling: SELECT * FROM alunos;

at com.example.guilherme.agentaaluno.dao.AlunoDAO.buscaAlunos(AlunoDAO.java:119)
                                                                                       at com.example.guilherme.agentaaluno.ListaAlunosActivity.carregaLista(ListaAlunosActivity.java:70)
                                                                                       at com.example.guilherme.agentaaluno.ListaAlunosActivity.onResume(ListaAlunosActivity.java:103)

O erro esta apontando para metodo:

public List<Aluno> buscaAlunos() {
        String sql = "SELECT * FROM Alunos;";
        SQLiteDatabase db = getReadableDatabase();
        Cursor c = db.rawQuery(sql, null);

        List<Aluno> alunos = populaAlunos(c);
        c.close();

        return alunos;
    }
solução!

Oi Guilherme, tudo bem?

Pelo o que eu verifiquei não é mais um problema relacionado ao recebimento dos alunos na aplicação. O que acontece agora é que o seu DAO está alarmando que não consegue encontrar a tabela alunos.

A minha sugestão seria verificar no DAO se existe alguma diferença no nome de tabela. Provavelmente por ter acontecido durante a migration... É comum de acontecer algum detalhe durante a migration, por isso é importante dobrar a atenção e verificar algo que pode ser problemático. De qualquer maneira, é possível, conter as informações da tabela que é criada desinstalando a aplicação, ou seja, você pode desinstalar a app e instalar novamente que o onCreate() do AlunoDAO será executado novamente.

Caso o problema ainda for no AlunoDAO, peço que abra um tópico novo especificando o problema para que outros alunos que tiverem o mesmo problema que o seu, vejam e consigam resolver a situação caso aconteça com eles também.

[]s

Deu certo, eu desinstalei app você estava certo por algum motivo ele não encontrava tabela alunos.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software