Reputation: 101
The PHP that gives me a list of records through a SQL returns me the following:
$result = $con->query($query);
for ($set = array (); $row = $result->fetch_assoc(); $set[] = json_encode($row));
print_r($set);
RESULTADO
Array
(
[0] => {"id":"8783","nombre":"pepe","username":"demo"}
[1] => {"id":"8784","nombre":"garcia","username":"demo"}
)
Now as a process that from Android with Volley, I have the following but obviously not working:
JsonArrayRequest request = new JsonArrayRequest(URL_2,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray jsonArray) {
for(int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String nombre = jsonObject.getString("name");
Log.e("nombre", nombre);
mEntries.add(jsonObject.toString());
}
catch(JSONException e) {
mEntries.add("Error: " + e.getLocalizedMessage());
}
ERROR:
com.android.volley.ParseError: org.json.JSONException: Value Array of Type java.lang.String cannot be converted to JSONArray
I modify the volley by leaving only the following within the try and it throws the same error
try {
Integer cantidad = jsonArray.length();
Log.e("cantidad: ", cantidad.toString());
}
I try with this and continue the same error
public void onResponse(JSONArray response) {
for(int i = 0; i < response.length(); i++) {
try {
Log.i("SUCCESS", response.toString());
}
.....
Upvotes: 0
Views: 569
Reputation: 5329
change your php file
$result = $con->query($query);
json_encode(["result" => $result]);
and in onResponse
add:
public void onResponse(JSONArray jsonArray) {
JsonArray myData = jsonArray.getJSONArray(0);
Upvotes: 1