Android-dev.it Community

 
Annunci

:arrow: Benvenuto!, se non sei ancora un utente registrato: puoi registrati qui, oppure effettua il login
:arrow: Apri un forum sul tuo nuovo smartphone Android e collabora con noi! (dettagli)

www.agendadigitale.org
Image


Sponsor
It is currently Thu May 24, 2012 8:08 am

All times are UTC + 1 hour


 Topics   Replies   Views   Last post 
No new posts Applicazione per gestire SMS

by filippo729 on Wed May 23, 2012 9:39 pm in Sviluppo e programmazione in Android

0

18

Wed May 23, 2012 9:39 pm

filippo729

No new posts На интернет-витрине

by popowahgjfghjoiuyt on Sat May 19, 2012 2:25 pm in Amministrazione

0

17

Sat May 19, 2012 2:25 pm

popowahgjfghjoiuyt

No new posts Apri un forum sul tuo nuovo smartphone Android!

[ Go to page: 1, 2, 3 ]

by ReattileGar on Fri May 18, 2012 5:27 pm in Amministrazione

27

12556

Fri May 18, 2012 5:27 pm

ReattileGar

No new posts Новый: базы для хрумера - профессиональный софт для СЕО.

by Renatafe on Fri May 18, 2012 3:14 pm in Amministrazione

0

27

Fri May 18, 2012 3:14 pm

Renatafe

No new posts Pace is undoubtedly the vital affordable Jerseys Wholesale d

by barnettmmolive2012 on Wed May 16, 2012 7:36 am in Amministrazione

1

49

Wed May 16, 2012 7:36 am

barnettmmolive2012




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Più parsing nello stesso file PHP
PostPosted: Wed Aug 24, 2011 3:24 pm 
Offline
User avatar

Joined: Wed Aug 24, 2011 3:16 pm
Posts: 2
Punti Android: ?
Salve gente,

Collegandomi più volte allo stesso file PHP contenente tutte le query che devo effettuare succede che ogni volta che eseguo una query diversa del file PHP il risultato che ottengo è ovviamente sempre diverso.
Ho creato una tabella in mysql contentente usrname e psw.
Dal file PHP eseguo:
una query che mi restituisce l'usrname
una query che mi restituisce la psw

Ora, avendo creato una funzione che funge da Parsing Json come faccio a fargli capire che una volta mi dovrà restituire il parsing dell'username restituito così: [{"usrname":"user1"}] e una volta il parsing della password restituito così: [{"psw":"psw1"}]

nel layout ho 2 bottoni:
1) si connette al PHP, esegue la QUERY e restituisce usrname passando dalla funzione di Parse
2) si connette al PHP, esegue la QUERY e restituisce psw passando dalla funzione di Parse

la funzione è questa:
Code:
private String parse(String s) {
      try {
         JSONArray array = new JSONArray(s);

         for (int i = 0; i < array.length(); i++) {
            jobj = array.getJSONObject(i);
         }

         user = jobj.getString("usrname");  //se clicco sul bottone 2 da errore questa riga
         paswd = jobj.getString("psw"); //se clicco sul bottone 1 da errore questa riga

         return paswd;
      } catch (JSONException e) {
         Log.e("Err", e.toString());
         return null;
      }
   }


se clicco sul bottone 1 da errore
Code:
08-23 12:48:22.459: ERROR/Err(1129): org.json.JSONException: No value for psw

Ovvio perchè quando il debug passa dalla funzione "parse" dal mio codice si aspetta di trovare un JsonObject che si chiami psw, ma avendo eseguito la query che da questo risultato: [{"usrname":"user1"}] non può trovarlo.

se clicco sul bottone 2 da quindi quest altro errore
Code:
08-23 12:48:22.459: ERROR/Err(1129): org.json.JSONException: No value for usrname


Qualcuno sa come sistemare la funzione per far si che non esca più l'errore?


Top
 Profile  
 
 Post subject: Re: Più parsing nello stesso file PHP
PostPosted: Wed Aug 24, 2011 4:22 pm 
Offline
Moderatore
User avatar

Joined: Thu Dec 27, 2007 3:51 pm
Posts: 986
Cellulare Android: HTC Dream
Operatore: TIM
Punti Android: 1320
Uhm.. quante comlicazioni per una cosa semplice...
Risposta breve:
- cattura la JSONException per ciascuna delle due istruzioni jobj.getString() in modo che quella che prova a chiedere la key non presente non interrompa il programma
oppure
- utilizza il metodo optString anzichè getString() che non genera eccezioni e restituisci solo l'elemento presente
Code:
user = jobj.optString("usrname", "non esiste");
paswd = jobj.optString("psw", "non esiste");


Risposta lunga; se non ho capito male la funzione parse può ricevere, a seconda dei casi, una di queste due stringhe:
{"usrname":"user1"}
{"psw":"psw1"}

Se è così, l'array ha sempre un solo elemento e quindi non ha senso il ciclo for

Code:
private String parse(String s) {
try {
jobj = ((JSONArray)new JSONArray(s)).getJSONObject(0);

user = jobj.optString("usrname", "non esiste");
paswd = jobj.optString("psw", "non esiste");

if (user != "non esiste")
  return user;
else if (paswd = "non esiste")
  return paswd;
else
  return "errore";
}


Top
 Profile  
 
 Post subject: Re: Più parsing nello stesso file PHP
PostPosted: Mon Aug 29, 2011 6:00 pm 
Offline
User avatar

Joined: Wed Aug 24, 2011 3:16 pm
Posts: 2
Punti Android: ?
Grazie mille della fulminea risposta :-) Non sapevo che ci fosse pure quella funzione. Chiedo scusa per non aver controllato meglio.
Ah, il 'for' l'ho messo perchè è possibile che debba aggiungere delle SELECT di PHP che diano come risultato più di un attributo ;-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
phpBB SEO

All trademarks and logos used in this site are of properties of their respective owners.