Thread calling method to query a web service always returns null

Asked

Viewed 213 times

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;
}
  • 2

    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.

  • 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).

  • First of all, why thread?

  • Because the search method accesses a web service to fill the list, so I run it in a separate thread.

  • 1

    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.

  • In a quick search, it seems that a good one for SOAP is this: http://simpligility.github.io/ksoap2-android/index.html

  • Blz. I’ll give a study on Asynctask.

Show 2 more comments

1 answer

0

The problem is in the thread. Since the thread execution is asynchronous, the method responds before the listNotas = notaWS.buscarNotas(matricula,stage) be 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 have to implement a callback method and remove the Return of the method lowborn

Browser other questions tagged

You are not signed in. Login or sign up in order to post.