2016-03-26 20 views
1

Ich habe Parameter auf meinem Server gesendet, aber im Gegenzug sie gesendete Nachricht Fehler,JSONObject kann nicht in JSONArray umgewandelt werden, wenn json anfordernden

JSONObject cannot be converted to JSONArray 

dies ist mein Code:

// Creating volley request obj 
    JsonObjectRequest taxiReq = new JsonObjectRequest (url, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.d(TAG, response.toString()); 

        try { 
         JSONArray taxiJsonArray = response.getJSONArray("taxi_list"); 
         // Parsing json 
         for (int i = 0; i < taxiJsonArray.length(); i++) { 
          JSONObject obj = taxiJsonArray.getJSONObject(i); 
          Taxi taxi = new Taxi(); 
          taxi.setTaxiname(taxiJsonArray.getString("taxiname")); 
          taxi.setThumbnailUrl(taxiJsonArray.getString("image")); 
          taxi.setdeparture(taxiJsonArray.getString("departure")); 
          taxi.setarrive(taxiJsonArray.getString("arrive")); 
          taxi.setseat(taxiJsonArray.getInt("seat")); 
          taxi.setcost(taxiJsonArray.getInt("cost")); 

          // adding taxi to taxi array 
          taxiList.add(taxi); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

        // notifying list adapter about data changes 
        // so that it renders the list view with updated data 
        adapter.notifyDataSetChanged(); 
       } 
      }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e(TAG, "Requesting Taxi Error: " + error.getMessage()); 
      Toast.makeText(getApplicationContext(), 
        error.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    }) 

und das ist mein json:

{ 
    "error": false, 
    "taxi_list": [ 
    { 
     "image": "http://localhost/androidapp/taxiprofile/1.jpg", 
     "taxiname": "Taxi 1", 
     "from": "PTK", 
     "to": "SGU", 
     "departure": "08:00:00", 
     "arrive": "13:00:00", 
     "seat": 7, 
     "cost": 12 
    }, 
    { 
     "image": "http://localhost/androidapp/taxiprofile/default.jpg", 
     "taxiname": "Taxi 2", 
     "from": "PTK", 
     "to": "SGU", 
     "departure": "08:00:00", 
     "arrive": "13:00:00", 
     "seat": 2, 
     "cost": 15 
    }, 
    { 
     "image": "http://localhost/androidapp/taxiprofile/2.jpg", 
     "taxiname": "Taxi Untung Selalu", 
     "from": "PTK", 
     "to": "SGU", 
     "departure": "09:00:00", 
     "arrive": "14:00:00", 
     "seat": 3, 
     "cost": 13 
    } 
    ] 
} 

ich habe versucht JSONObject zu JSONArray zu ändern, aber es mit Fehlern noch kommen ...vielleicht, weil ich versuchte Objekt zu bekommen, aber es gibt nicht ein ...

EDIT: neue Fehler, nachdem ich den Code geändert

error: incompatible types: String cannot be converted to int 

in line:

taxi.setTaxiname(obj.getString("taxiname")); 

irgendeine Hilfe?

+0

was die json aussehen wird? – TooManyEduardos

+0

Bitte überprüfen Sie Ihre JSON-Antwort Format..Ihr Code ist perfekt ... nach mir –

+0

Ich habe meinen Code aktualisieren, keine Hilfe ... – meeftah

Antwort

0

Ihre "JSONArray Antwort" ist kein JsonArray Es ist ein JsonObject versuchen diese

für eine JSONArray
public void onResponse(JSONObject response) { 
         Log.d(TAG, response.toString()); 
         JSONArray responceArray = response.getJSONArray(); 

         // Parsing json 
         for (int i = 0; i < responceArray.length(); i++) { 

           ------------- Your code------------ 
          } 
         } 
0

Sie erwarten einmal, aber Ihr Top-Level-Element ist eigentlich ein Objekt:

{  <--- This is a JSON Object 
    "error": false, 
    "taxi_list": [ 
    { 
     "image": "http://localhost/androidapp/taxiprofile/1.jpg", 
     "taxiname": "Taxi 1", 
     "from": "PTK", 
     "to": "SGU", 
     "departure": "08:00:00", 
     "arrive": "13:00:00", 
     "seat": 7, 
     "cost": 12 
    }, 

Sie müssen dies ändern:

JsonArrayRequest taxiReq = new JsonArrayRequest(url, 
     new Response.Listener<JSONArray>() 

um stattdessen ein JsonObject anfordern.

1

müssen Sie Ihre Volley Anfrage an JsonObjectRequest ändern. So wird Ihr Code sein:

+0

neue Fehler auftauchen, inkompatible Typen: String kann nicht in int in Zeile umgewandelt werden: taxi.setTaxiname (obj.getString ("taxiname")); – meeftah

0

Machen Sie StringRequest anstelle von JSONObjectRequest und verwenden Sie diese Methode wird es gut funktionieren.

Probleme im Code:

  1. Sie wurden Werte direkt aus dem Array und nicht von JSONObject nehmen.
  2. Sie nicht auf die Antwort von dem Server überprüft wurde korrekt in JSON formatiert oder nicht überprüft. StringRequest wird Ihnen dabei helfen.

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { 
 
     @Override 
 
     public void onResponse(String s) { 
 
      Log.d(TAG, response.toString()); 
 

 
      try { 
 
       JSONObject jsonObject = new JSONObject(s); 
 

 
       if (jsonObject.getString("error").equals("false")) { 
 
        JSONArray taxiJsonArray = jsonObject.getJSONArray("taxi_list"); 
 
        // Parsing json 
 
        for (int i = 0; i < taxiJsonArray.length(); i++) { 
 
         JSONObject obj = taxiJsonArray.getJSONObject(i); 
 
         Taxi taxi = new Taxi(); 
 
         taxi.setTaxiname(obj.getString("taxiname")); 
 
         taxi.setThumbnailUrl(obj.getString("image")); 
 
         taxi.setdeparture(obj.getString("departure")); 
 
         taxi.setarrive(obj.getString("arrive")); 
 
         taxi.setseat(obj.getInt("seat")); 
 
         taxi.setcost(obj.getInt("cost")); 
 

 
         // adding taxi to taxi array 
 
         taxiList.add(taxi); 
 
        } 
 
       } 
 
      } catch (JSONException e) { 
 
       e.printStackTrace(); 
 
      } 
 

 
      // notifying list adapter about data changes 
 
      // so that it renders the list view with updated data 
 
      adapter.notifyDataSetChanged(); 
 
     } 
 
    }, new Response.ErrorListener() { 
 
     @Override 
 
     public void onErrorResponse(VolleyError volleyError) { 
 
      Log.e(TAG, "Requesting Taxi Error: " + error.getMessage()); 
 
      Toast.makeText(getApplicationContext(), 
 
        error.getMessage(), Toast.LENGTH_LONG).show(); 
 
     } 
 
    });

Verwandte Themen