2017-07-18 109 views
0

,让我的记录列表通过SQL的PHP​​返回我的以下内容:Android的凌空JsonArrayRequest

$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"} 
) 

现在,作为一个过程,在Android与排球,我有以下的,但显然不工作:

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()); 
            } 

错误:

com.android.volley.ParseError: org.json.JSONException: Value Array of Type java.lang.String cannot be converted to JSONArray

我的只剩下机智修改凌空欣尝试,并抛出了同样的错误

try { 
    Integer cantidad = jsonArray.length(); 
    Log.e("cantidad: ", cantidad.toString()); 
} 

我尝试这一点,并继续同样的错误

public void onResponse(JSONArray response) { 
    for(int i = 0; i < response.length(); i++) { 
     try { 
     Log.i("SUCCESS", response.toString()); 
     } 
..... 
+0

什么是你的questi上? –

回答

1

改变你的PHP文件

$result = $con->query($query); 
json_encode(["result" => $result]); 

onResponse加:

public void onResponse(JSONArray jsonArray) { 
    JsonArray myData = jsonArray.getJSONArray(0); 
+0

Lo pruebo con Postman y me arroja esto {“result”:{“current_field”:null,“field_count”:null,“lengths”:null,“num_rows”:null,“type”:null}} – desarrollosTELLO

+0

Con ($ query2); $ result = $ con> query($ query2); echo json_encode([“result”=> $ result]); – desarrollosTELLO

+0

英文请 –