1
When I call this method to follow in another class it is returning null. However I have already performed the debug in the line "Return listNotes;" and it’s being filled normally.
Method code:
ArrayList<Nota> listNotas = new ArrayList<>();
public ArrayList buscarNotas(String matricula, int etapa){
try {
SoapObject resposta = new SoapObject(NAMESPACE, METHOD_NAME);
resposta.addProperty("Matricula", matricula);
resposta.addProperty("Etapa", etapa);
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++ ) {
Nota nota = new Nota();
JSONObject jsonObject =jsonArray.getJSONObject(i);
nota.setDisciplina(jsonObject.getString("Materia"));
nota.setNota(jsonObject.getString("VlrNota"));
listNotas.add(i,nota);
}
} 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 listNotas;
}
Method call in another class. Note: where you are receiving null
public List<Nota> baixarNotas (final String matricula, final int etapa){
String msg = "Carregando";
String titulo = "Aguarde";
final ProgressDialog dialog = ProgressDialog.show(this, titulo, msg);
new Thread(new Runnable() {
@Override
public void run() {
try {
listaNotas= notaWS.buscarNotas(matricula,etapa);
} catch (Exception e) {
} finally {
dialog.dismiss();
}
}}).start();
return listaNotas;
}
Hello, Alan. Please start by adjusting the formatting as it is difficult to understand the problem when the code is hard to read. Your problem is probably the asynchronous nature of the threads. You cannot simply return a value that will be fetched in another thread, as you will return something not quite ready long before the other thread actually runs and does the process you want it to.
– Pablo Almeida
Hello, Pablo! Thank you so much for the attention and the tip about formatting. In the case of Return, how do I get it right? (I’m a beginner in java).
– alannrs
First of all, why thread?
– Pablo Almeida
Because the search method accesses a web service to fill the list, so I run it in a separate thread.
– alannrs
Even so, there is no reason to create threads like this in an Android code. The ideal would be to use an Asynctask or, better yet, use a library to do this job. Threads have several problems on Android. The one you’re facing is just one of them.
– Pablo Almeida
In a quick search, it seems that a good one for SOAP is this: http://simpligility.github.io/ksoap2-android/index.html
– Pablo Almeida
Blz. I’ll give a study on Asynctask.
– alannrs