vitordelfino

Requisicao webservice para obtencao do menor caminho

Apr 30th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. /* metodo que faz a requisição ao web service e retorna as estações percorridas
  2.    com origem e destino selecionado */
  3. public void clicar(View src){
  4.  
  5.         // preenche a posicao das estacoes de entrada e saida
  6.         entradaSelecionada = entradaEstacoes.getSelectedItemPosition();
  7.         saidaSelecionada = saidaEstacoes.getSelectedItemPosition();
  8.  
  9.         //monsta objeto json a ser enviado na requisicao
  10.         JSONObject joOrigemDestino = new JSONObject();
  11.         try{
  12.             joOrigemDestino.put("origem",estacoesObj.get(entradaSelecionada).getNomeEstacao());
  13.             joOrigemDestino.put("destino",estacoesObj.get(saidaSelecionada).getNomeEstacao());
  14.             Log.d("", "clicar: " + joOrigemDestino.toString());
  15.         }catch(JSONException e){
  16.             e.printStackTrace();
  17.         }
  18.        
  19.         //objeto usado para preencher as estações retornadas
  20.         final ArrayList<Estacao> estacoes = new ArrayList<Estacao>();
  21.        
  22.         //requisicao ao webservice
  23.         RealizaRequisicao.getInstance().postJson(Simulador.this, urlSimulador(),joOrigemDestino,new VolleyCallbackObject() {
  24.             @Override
  25.             public void onSuccess(JSONObject result) {
  26.                 try{
  27.                     JSONArray array = result.getJSONArray("estacao");
  28.                     for(int i = 0; i < array.length(); i++){
  29.                         JSONObject jo = array.getJSONObject(i);
  30.                         try{
  31.                             estacoes.add(new Estacao(Integer.parseInt(jo.getString("codEstacao")),
  32.                                 Integer.parseInt(jo.getString("linha")),
  33.                                 jo.getString("nome")));
  34.                         }catch(JSONException ex){
  35.                             ex.printStackTrace();
  36.                         }
  37.  
  38.                     }
  39.                 }catch (JSONException e){
  40.                     try{
  41.                         result = result.getJSONObject("estacao");
  42.                         estacoes.add(new Estacao(Integer.parseInt(result.getString("codEstacao")),
  43.                                 Integer.parseInt(result.getString("linha")),
  44.                                 result.getString("nome")));
  45.                     }catch(JSONException ex2){
  46.                         ex2.printStackTrace();
  47.                     }
  48.  
  49.                 }
  50.                 //metodo que preenche o layout com as estações
  51.                 montarListaEstacoes(estacoes);
  52.             }
  53.         });
  54.     }
Advertisement
Add Comment
Please, Sign In to add comment