1
The first thing I do in my application is send a message to the server, only to test if the connection is active and working. When receiving "ok" from the server I continue the application.
On the next screen I have to show a list of names, so I request the list. But it shows nothing the first time I start the screen, for something to appear I need to reload the screen, as 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 appearing at first because the content she was receiving was "ok", which I get when making the connection check. I also identified that the Log responsible for showing what is coming from the requests made (in the function below attached) was correct, showing having 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 information of the function below. How is that possible? I called a function and put an object to pick up the return, but the object has a value set before the return of my function. And this set value is the value of my last request.
I am 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. Also because it is the >first, already the request of the list that is the 2nd picks the answer of the 1st >request. If I reload the page this means that a new >request is made, right? That would be the list request. Now the list appears, >but it appears because she got the answer to the second request, which is also >a list. The answer to the 3rd request was not used, and if I make a >4th request this will take the information from the 3rd and so on.
Below is the code used to make the requests.
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 sending 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 asks for the list of names for the function that uses 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;
}
The question is very confusing, simplify, put the code(s) where the problem occurs.
– rubStackOverflow
I tried to explain as much as I could, I edited it out to see if it looked better. It’s really confusing, but I don’t know how to explain it better.
– Raphael Schmitt