i have a problem with my code and other post cannot helped me that's why i post here. I'm getting some json from my php like
{
"lesMots":{
"1":{
"id":"4",
"wordFrench":"bite",
"wordEnglish":"dick"
},
"2":{
"id":"7",
"wordFrench":"pute",
"wordEnglish":"whore"
},
"3":{
"id":"2",
"wordFrench":"ordinateur",
"wordEnglish":"computer"
},
"4":{
"id":"1",
"wordFrench":"bonjour",
"wordEnglish":"hello"
},
"5":{
"id":"3",
"wordFrench":"monde",
"wordEnglish":"world"
}
}
}
Here is my Volley Request, i know i must use JSONObject for my JSONArray, but when i change it, the second JSONObject word ask me string and not int ! i'm trying to get 5 random french word and english word
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://ent-ifsi.com/Projet/Application_Android/pendu_android.php",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("lesMots");
for (int i = 0; i < jsonArray.length();i++){
JSONObject word = jsonArray.getJSONObject(i);
String wordFrench = word.getString("wordFrench");
String wordEnglish = word.getString("wordEnglish");
textView.append(wordFrench+" "+wordEnglish+" "+" \n ");
Toast.makeText(getApplicationContext(), wordFrench +"",Toast.LENGTH_LONG).show();
}
}catch (JSONException e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), e +"",Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "ERROR");
Toast.makeText(getApplicationContext(), error +"",Toast.LENGTH_LONG).show();
}
}
);
queue.add(jsonObjectRequest);
}
});
Thank for your help, i'm blocked on this problem and i'm kind of new in this. If you need something more tell me
lesMotsis not JsonArray, itsJsonObject, There is noJsonArrayin your response. Please check difference betweenJsonObjectandJsonArray.try { JSONObject jsonArray= response.getJSONObject("lesMots"); for (int i = 0; i < jsonArray.length();i++){ JSONObject word = jsonArray.getJSONObject(i);he ask me an string on the i but i don't know what to put here