0
I have a class responsible for downloading a list of students in a web service through a method where I pass as parameter a string with the teacher’s code and it returns me an array of students. The problem is that when I call this method in the other class it is returning me an empty array.
I’ve already debugged the Return method of the web service and it has all the data, only when I receive it in another class it is empty.
Follow the code:
WEBSERVICE CLASS WITH THE METHOD THAT RETURNS STUDENTS
ArrayList<Aluno> listAlunos = new ArrayList<>();
public ArrayList<Aluno> alunos(String codProfessor){
try {
SoapObject resposta = new SoapObject(NAMESPACE, METHOD_NAME);
resposta.addProperty("CodigoPro", codProfessor);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(resposta);
HttpTransportSE http = new HttpTransportSE(URL);
http.call(SOAP_ACTION, envelope);
String resultado = envelope.getResponse().toString();
JSONArray jsonArray = new JSONArray(resultado);
for(int i=0;i<jsonArray.length();i++ ) {
Aluno aluno = new Aluno();
JSONObject jsonObject =jsonArray.getJSONObject(i);
aluno.setMatriculaAluno(jsonObject.getString("Matricula"));
aluno.setNomeAluno(jsonObject.getString("Nome"));
aluno.setDisciplinaAluno(jsonObject.getString("CodMat"));
aluno.setTurmaAluno(jsonObject.getString("CodTur"));
listAlunos.add(i,aluno);
}
} catch (HttpResponseException e) {
e.printStackTrace();
} catch (SoapFault soapFault) {
soapFault.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return listAlunos; // FOI REALIZADO O DEBUG DESSA LINHA, ESTÁ OK COM TODOS OS DADOS
}
CLASS WHERE I CALL THE METHOD SO THAT WHEN THE ACTIVITY IS CREATED IT DOES RETRIEVES THAT LIST OF STUDENTS AND PASSES TO DAO CLASS.
AlunoDAO alunoDAO = new AlunoDAO(getBaseContext());
ArrayList<Aluno> listaAlunos = new ArrayList<Aluno>();
AlunoWS alunoWS = new AlunoWS();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
String nomeProfessor = getIntent().getStringExtra("Professor");
baixarDados(nomeProfessor);
}
public void baixarDados(final String codProfessor){
String msg = "Atualizando dados";
String titulo = "Aguarde";
final ProgressDialog dialog = ProgressDialog.show(this, titulo, msg);
new Thread(new Runnable() {
@Override
public void run() {
try {
listaAlunos = alunoWS.alunos(codProfessor); //LISTA ALUNOS ESTÁ RECEBENDO NULL
alunoDAO.insereAlunos(listaAlunos);
} catch (Exception e) {
} finally {
dialog.dismiss();
}
}
}).start();
}
No need for you to indicate the index that will be added to the object:
listAlunos.add(i,aluno);
->listAlunos.add(aluno);
– Nayron Morais
And also define the type of object that will be implemented in the interface of the list, in the first class is like this:
ArrayList<Aluno> listAlunos = new ArrayList<>();
And on Monday it’s like thisArrayList<Aluno> listAlunos = new ArrayList<Aluno>();
– Nayron Morais
Look, you’re running the method within a thread, but in this case you should have something synchronous since you depend on the return of your webservice. Try to take it out of the thread just to test. If it works, then you put it into an Asynctask, but this in the webservice and not in Activity.
– Giuliana Bezerra