NunoDinis Posted November 26, 2012 Report Share Posted November 26, 2012 Bom dia, estou com um problema aqui na minha aplicação. Com esta função vou buscar através de um ficheiro PHP os valores (através de uma query). Por exemplo, vou buscar o ac, referencia e a cor, que transporto assim : ValorAC#ValorRef#ValorCor#^ ( ex concreto : 152#234#vermelho#^) . Depois separo a string e meto os valores num array, para depois os meter nas respetivas caixas de texto. A primeira vez dá perfeitamente, mas caso me tenha enganado e/ou queira trocar a ordem para efetuar nova procura, dá-me erro. public void DadosSAP() { String urlGetSAP="http://192.168.206.110:82/android/SAP.php?ordem=" + etOrdem.getText().toString(); String respostaRetornadaSAP = null; try { respostaRetornadaSAP = ConexaoHttpClient.executaHttpGet(urlGetSAP); String respostaSAP = respostaRetornadaSAP.toString(); if(respostaSAP.contains("A ordem")) { Intent it = new Intent(Simple.this, Main.class); finish(); it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Simple.this.startActivity(it); Toast.makeText(this, "Não existe a ordem " + etOrdem.getText().toString() + " em SAP", Toast.LENGTH_LONG).show(); } else { char separadarorSAP='#'; int contaUsuariosSAP=0; for(int i=0;i<respostaSAP.length();i++) if(separadarorSAP == respostaSAP.charAt(i)) contaUsuariosSAP++; listaUsuariosSAP = new String[contaUsuariosSAP]; char caracter_lidoSAP=respostaSAP.charAt(0); String nomeSAP=""; for (int i=0;caracter_lidoSAP != '^';i++) { caracter_lidoSAP = respostaSAP.charAt(i); if (caracter_lidoSAP != '#') { nomeSAP+= (char) caracter_lidoSAP; //Log.i("NOME","NOME::" + nome); } else { listaUsuariosSAP[posicaoSAP]=""+nomeSAP; posicaoSAP++; //Log.i("NOME2","NOME2::" + nomeSAP); nomeSAP=""; etAc.setText(listaUsuariosSAP[0]); etCor.setText(listaUsuariosSAP[1]); listaUsuariosSAP[2]=""; etReferencia.setText(listaUsuariosSAP[3]); listaUsuariosSAP[4]=""; } } } } catch(Exception erro) { Log.i("erro 1", "erro 1= "+erro); } } Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro. ndsotware.org Link to comment Share on other sites More sharing options...
KTachyon Posted November 26, 2012 Report Share Posted November 26, 2012 Era melhor se utilizasses JSON ou XML para efectuar o transporte: { "ac" : 152 , "ref" : 234 , "cor" : "vermelho" } A maioria das bibliotecas de JSON iriam converter isto para um HashMap e só tinhas que ir buscar o valor que corresponde à chave. De qualquer forma, para separares a string para um array de strings só precisas de utilizar o split. “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare Link to comment Share on other sites More sharing options...
NunoDinis Posted November 26, 2012 Author Report Share Posted November 26, 2012 Podes-me indicar um tutorial para que possa perceber o que devo fazer no ficheiro PHP ? Isto porque nunca utilizei JSON ... Basta fazer o encode do JSON ? print(json_encode($liista[0])); Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro. ndsotware.org Link to comment Share on other sites More sharing options...
NunoDinis Posted November 26, 2012 Author Report Share Posted November 26, 2012 De qualquer forma, para separares a string para um array de strings só precisas de utilizar o split. Realmente resolveu ((: Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro. ndsotware.org Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now