3
I make 3 requests in a row, but they are fired in processing order, not synchronously, how can I resolve this?
...
//botão pressionado
// primeira requisição
ivUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* auto completes */
txt_estados = (AutoCompleteTextView)
findViewById(R.id.txt_estado);
txt_cidades = (AutoCompleteTextView)
findViewById(R.id.txt_cidade);
txt_bairros = (AutoCompleteTextView)
findViewById(R.id.txt_bairro);
final String estado = txt_estados.getText().toString().trim();
final String cidade = txt_cidades.getText().toString().trim();
final String bairro = txt_bairros.getText().toString().trim();
final String metrosQ = txt_metrosQ.getText().toString().trim();
final String quartos = txt_quartos.getText().toString().trim();
final String garagens = txt_garagens.getText().toString().trim();
final String cep = txt_cep.getText().toString().trim();
final String complemento = txt_complemento.getText().toString().trim();
final String preco = txt_preco.getText().toString().trim();
final String descricao = txt_descricao.getText().toString().trim();
final String map = txt_map.getText().toString().trim();
final RadioGroup Rdtipo = (RadioGroup) findViewById(R.id.rd_tipo);
int ntipo = Rdtipo.getCheckedRadioButtonId();
if (ntipo == R.id.Apartamento) {
tipo = "Apartamento";
} else if (ntipo == R.id.Barracao) {
tipo = "Barracão";
}else if (ntipo == R.id.Residencial) {
tipo = "Residêncial";
}else if (ntipo == R.id.Terreno) {
tipo = "Terreno";
}else if (ntipo == R.id.Kitinete) {
tipo = "Kitinete";
}
final RadioGroup RdtipoNeg = (RadioGroup) findViewById(R.id.rd_tipoNeg);
int ntipoNeg = Rdtipo.getCheckedRadioButtonId();
if (ntipoNeg == R.id.A) {
tipoNeg = "A";
} else if (ntipoNeg == R.id.C) {
tipoNeg = "C";
}
final String idUser = "51"; // <-- aqui o id do usuario
final MyCommand myCommander = new MyCommand(getApplicationContext());
try{
String url = "http://a4da1ea4.ngrok.io/upVariasImgs/solicitarAvaliacao.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
idImovel = response;
responseList.add(response);
cadImgs(); <-- chamo a segunda requisição
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Erro ao fazer upload da imagem", Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("metrosQ", metrosQ);
params.put("quartos", quartos);
params.put("garagens", garagens);
params.put("cep", cep);
params.put("complemento", complemento);
params.put("descricao", descricao);
params.put("mapa", map);
params.put("estado", estado);
params.put("cidade", cidade);
params.put("bairro", bairro);
params.put("tipo", tipo);
params.put("tipoNeg", tipoNeg);
params.put("preco", preco);
params.put("idUser", idUser);
return params;
}
};
myCommander.add(stringRequest);
} catch (NullPointerException e) {
Toast.makeText(getApplicationContext(), "Erro ao fazer upload da imagem", Toast.LENGTH_SHORT).show();
}
myCommander.execute();
}
});
}
when pressing the ivUpload button, the first request is triggered, where the property is saved in the database and returned its id by json
//segunda requisição
public void cadImgs() {
final MyCommand myCommand = new MyCommand(getApplicationContext());
for(String imagePath: imageList){
try {
Bitmap bitmap = PhotoLoader.init().from(imagePath).requestSize(512, 512).getBitmap();
final String encodedString = ImageBase64.encode(bitmap);
String url = "http://a4da1ea4.ngrok.io/upVariasImgs/upload.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Erro ao fazer upload da imagem", Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("image", encodedString);
params.put("idImo", idImovel);
return params;
}
};
myCommand.add(stringRequest);
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), "Erro ao fazer upload da imagem", Toast.LENGTH_SHORT).show();
}
}
myCommand.execute();
somaId(); <-- está executando antes do loop acima
}
//terceira requisição
public void somaId(){
...
}
soon after I pick up this id for the second request, cadImgs() method, which is fired in the json response of the first request, until then it follows the order, but the second request is within a loop
for this reason I did not call the third function somaId() in the server response but after the loop ended, which causes it to break this order, and perform the function somaId, before the loop
Only you make the next request only after you get the answer of the first, and so on.
– viana
I updated the code in order, but the problem persists, I have no idea what it might be.
– Felipe Duarte
But what is the problem, if you do not put in your question, there is no way to solve it? What error is giving?
– viana
@Acklay is just the order, I make 3 requests to a server, but they have to be in order because one depends on the other, the first caught an id that I use in the second and so on.
– Felipe Duarte
Is there any way you can place the order that should be done here in the comments? Is it in the order of insertion in the code? That’s when you click the button?
– viana
See if that makes sense to you? That is exactly what I meant in the first comment: https://gist.github.com/cleidimarviana/cf74bc5f6e1c9e0e936b5519f60703e3
– viana
I just explained the order and actions in the code
– Felipe Duarte
Let’s go continue this discussion in chat.
– viana