Method that calls thread to query a web service always returns null

1

When I call this method in another class it is returning null. But I've debugged the "return listNotes;" line and it's currently being populated.

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;

}

Call the method in another class. Note: where you are getting 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;
}
    
asked by anonymous 20.09.2016 / 19:55

1 answer

0

The problem is in the thread. Since the execution of the thread is asynchronous, the method responds before the listNotes = noteWS.searchNotes (matricula, step) is executed.

Look at the code below.

public List<Nota> baixarNotas (final String matricula, final int etapa){
    String msg = "Carregando";
    String titulo = "Aguarde";
    final ProgressDialog dialog = ProgressDialog.show(this, titulo, msg);

    try {
        listaNotas= notaWS.buscarNotas(matricula,etapa);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        dialog.dismiss();
    }

    return listaNotas;
}

If you want to keep the thread you will need to implement a callback method and remove the return from the DownloadNotes     

13.12.2016 / 22:12