Erreur "org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject"
Bonjour,
Je suis en train de développer un application Android sous Android Studio et j'utilise du JSON pour pouvoir accéder à ma base de données sous phpMyAdmin. Et je souhaite afficher des données sur mon application, plus précisément dans un GridView.
Le problème est que l'application reçoit bien les données, je peux le voir dans ma console. Mais en suite, il me signale un problème de conversion, que je n'arrive pas à résoudre et à trouver la source.
Voici les données que je reçois et qui est suivi directement par l'erreur.
Citation:
D/Response: {"valeurs":[{"col1":"test1","col2":"test2","col3":"test3"}],"success":1}
W/System.err: org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject
Et voici mon code.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| protected Void doInBackground(Void... params) {
Log.i("add", " start doInBackground");
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(urlGet, ServiceHandler.GET);
Log.d("Response: ",jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// return value of success
success=jsonObj.getInt("success");
Log.i("success", String.valueOf(success));
if (success==0)
{
// success=0 ==> there is a string = message
message=jsonObj.getString("message");
Log.i("message", message);
}
else if (success==1)
{
// success=1 ==> there is an array of data = valeurs
JSONArray dataValues = jsonObj.getJSONArray("valeurs");
// loop each row in the array
for(int j=1;j<dataValues.length();j++)
{
JSONObject values = dataValues.getJSONObject(j);
// return values of col1 in valCol1
String valCol1= values.getString("col1");
// return values of col2 in valCol2
String valCol2= values.getString("col2");
String valCol3= values.getString("col3");
//add a string witch contains all of data getted from the response
myListofData.add(valCol1+" - "+valCol2+" - "+valCol3);
Log.i("Row "+(j+1), valCol1+" - "+valCol2+" - "+valCol3);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Aucunes données à cette adresse");
}
Log.i("add", " end doInBackground");
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i("add", "onPostExecute");
super.onPostExecute(result);
if (progressDialog.isShowing())
{
progressDialog.dismiss();
}
if(success==1)
{
Toast.makeText(getApplicationContext(), "Bien récues ", Toast.LENGTH_LONG).show();
// show the list view contains the data
arrayadp = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_single_choice, myListofData);
lv.setAdapter(arrayadp);
}
else
{
Toast.makeText(getApplicationContext(), "Erreur", Toast.LENGTH_LONG).show();
}
} |
Je vous remercie d'avance pour votre aide.