I'm really new about android development.... I have this JSONObject:
[
{
"IDalunno":"1",
"Username":"federicove",
"Password":"5ecb232b535685f95fa7bc19e436c79e",
"Nome":"Federico",
"Cognome":"Vergallo",
"Classe":"5",
"Sezione":"A",
"Indirizzo":"Informatica Abacus"
},
{
"0":{
"DataAssenza":"2013-09-26",
"TipoAssenza":"Ritardo"
},
"2":{
"DataAssenza":"2013-12-11",
"TipoAssenza":"Assenza"
},
"4":{
"DataAssenza":"2013-12-16",
"TipoAssenza":"Assenza"
}
},
[
{
"Materia":"Italiano",
"DataVoto":"2014-04-01",
"TipoVoto":"Scritto",
"Voto":"8"
},
{
"Materia":"Italiano",
"DataVoto":"2014-04-02",
"TipoVoto":"Orale",
"Voto":"7"
},
{
"Materia":"Storia",
"DataVoto":"2014-04-09",
"TipoVoto":"Orale",
"Voto":"7"
},
{
"Materia":"Inglese",
"DataVoto":"2014-03-04",
"TipoVoto":"Orale",
"Voto":"9"
},
{
"Materia":"Inglese",
"DataVoto":"2014-03-10",
"TipoVoto":"Orale",
"Voto":"8"
},
{
"Materia":"Matematica",
"DataVoto":"2014-02-03",
"TipoVoto":"Scritto",
"Voto":"8"
}
]
]
This is my method:
protected String[] doInBackground(String...params) {
String url = "http://federicovergallo.altervista.org/blog/prova.php";
JSONParser sitoLogin = new JSONParser();
try{
JSONObject obj = sitoLogin.getJSONFromUrl(url, params);
String[] risultatoLogin = new String [3];
risultatoLogin[0] = obj.getString("IDalunno");
risultatoLogin[1] = obj.getString("Nome");
risultatoLogin[2] = obj.getString("Cognome");
return risultatoLogin;
//this doesn't work, it returns null everytime
}
catch (Exception e){
}
return null;
}
This is my parser:
// costruttore
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, String[] parametri) {
// Preparo i dati da passare tramite POST
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", parametri[0]));
params.add(new BasicNameValuePair("password", parametri[1]));
// HTTP request
Log.i("User", parametri[0]);
Log.i("Password", parametri[1]);
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httppost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Errore nella conversione " + e.toString());
}
//trasformo la stringa in un oggetto JSON
//se abbiamo fatto errori lato server, qui vengono fuori ;)
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Errore nel parse " + e.toString());
}
// return l'oggetto JSONObject
return jObj;
}
}
I have to save all information (from "IDalunno" and "Username" to "TipoVoto" and "Voto") in a String Array but I really don't know how ... Every hint could be precious.
EDIT: i've found out, I had to return a JSONArray in my method getJSONFromUrl and then access to the array using obj.getJSONObject(0).getString("IDalunno")