Second Web Service response still has the value of the first

1

The first thing I do in my application is to send a message to the server, just to test if the connection is up and running. When I receive "ok" from the server I continue the application.

On the next screen I have to show a list of names, so I make the list request. But it shows nothing the first time I start the screen, so that something appears I need to reload the screen, like when I turn the cell phone from portrait to landscape. It also appears if I put a button that forces the screen to be recreated.

After some tests I identified that the list was not showing up first because the content it was receiving was the "okay", which I get when doing the connection check. I also identified that the Log responsible for showing what is coming from the requests made (in the function attached below) was correct, showing that I received the list of names, but I also noticed that it is being shown after the Log that I put in the function that takes the function information below. How is this possible? I've called a function and put an object to get the return, however the object has a value set before the return of my function. And this set value is the value of my last request.

I'm using this library: compile org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2

  

Example: the initial request takes the right answer, hers. Because the > is the first, the list request that is the 2nd picks up the response of the 1st > requisition. If I reload the page this means that a new > request is made, right? That would be the requisition of the list. Now the list appears, > but it appears because it picked up the response from the 2nd requisition, which is also > a list. The response from the 3rd requisition was not used, and if I do a & quot; 4th requisition this will pick up information from the 3rd and so on.

Below is the code used to make the requisitions.

public class HttpConnection {
    public static String getSetDataWeb(String url, String method, String data) throws URISyntaxException {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        String answer = "";

        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            // Setamos nossa URI
            request.setURI(new URI(url));
            // Executamos nossa transação HTTP
            HttpResponse response = httpclient.execute(request);
            // Pegamos o conteúdo advindo como resposta e inserimos em um InputStream
            answer = EntityUtils.toString(response.getEntity());
            Log.d("RESPOSTAhttp", answer);    
        }
        catch(NullPointerException e){ e.printStackTrace(); }
        catch(ClientProtocolException e){ e.printStackTrace(); }
        catch(IOException e){ e.printStackTrace(); }
        return(answer);
    }
}

Function that sends the requested URL to httpConnection

public static String comandaDetalhes(final String numCartao) throws SQLException {
        new Thread(){
            public void run(){
                try{
                    n = HttpConnection.getSetDataWeb("http://192.168.1.20:7070/comanda/detalhes/"+numCartao, "", "");
                } catch(Exception e) {

                }
            }
        }.start();

        return n;
    }

Function that requests the list of names for the function using httpConnection

public List<Conteudo> getComandaDetalhes(){
        conteudosL = new ArrayList<>();
        //--------INICIO PEGAR ComandaS---------
        if(chamadas == 0){
            try {
                String teste = ComandaDAO.comandaDetalhes(getIntent().getStringExtra("NumeroCartao"));
                Log.d("DETALHESCOMANDA", teste);
                if(teste != "Comanda inexistente!"){
                    JSONObject jo = new JSONObject(teste);
                    chamadas =0;
                    JSONArray jaBebidas = new JSONArray(jo.getString("bebidas"));
                    Log.d("RESPOSTADetalhesBebidas", String.valueOf(jaBebidas));
                    JSONArray jaPratos = new JSONArray(jo.getString("pratos"));
                    Log.d("RESPOSTADetalhesPratos", String.valueOf(jaPratos));
                    JSONArray jaComanda = new JSONArray(jo.getString("comanda"));
                    Log.d("RESPOSTADetalhesComanda", String.valueOf(jaComanda));

                    for(int i = 0; i<jaBebidas.length(); i++){
                        jo = jaBebidas.getJSONObject(i);
                        Conteudo conteudos = new Conteudo();
                        conteudos.setNome("x" + jo.getInt("quantidade_bebidas_comanda")
                                + "  " + jo.getString("nome_bebida") + "  R$ "
                                + jo.getDouble("preco_bebida"));
                        conteudosL.add(conteudos);
                    }
                    for(int i = 0; i<jaPratos.length(); i++){
                        jo = jaPratos.getJSONObject(i);
                        Conteudo conteudos = new Conteudo();
                        conteudos.setNome("x" + jo.getInt("quantidade_pratos_comanda")
                                + "  " + jo.getString("nome_prato") + "  R$ "
                                + jo.getDouble("preco_prato"));
                        Log.d("PratoAdicionado", conteudos.getNome());
                        conteudosL.add(conteudos);
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return conteudosL;
    }
    
asked by anonymous 23.07.2016 / 17:05

1 answer

0

From what I'm seeing, in your comandaDetalhes function, you start a Thread but immediately returns that variable n , which is the variable that will receive the value of the response when it finishes, hoping that n already has the expected value (or returns only when it has). This is not going to happen.

In the second call to comandaDetalhes , the value of n returned the value that was assigned in the first response, that is, "OK". What you need to understand here is that return n (probably) runs before from

n = HttpConnection.getSetDataWeb("http://192.168.1.20:7070/comanda/detalhes/"+numCartao, "", "");

Asynchronous programming is different. It's no use trying to turn a code that uses Threads into a sequential code that will not work. It is best to learn the paradigm of truth otherwise you will always fall into these traps.

In addition, you have other problems, the most alarming being that you are swallowing exceptions. This is a huge foot shot, because when the bug appears (and it will > appear), you will need all the information and error messages that you can get.

    
23.07.2016 / 19:49