2016-03-29 6 views
1

ich einen Fehler von Post-Parameter zu meinem Server habe, es ist wie meine params sind gar nicht auf meinem Server vorbei ... alles was ich habe ist:Volley, keine Parameter in JsonArrayRequest vorbei

D/Searching: Error: org.json.JSONException: Value {"error_msg":"please choose city to view taxi list"} 

dass Nachricht nur kommen, wenn isset ($ _ POST [ 'aus'] so in meinem server

hier mein Code:

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

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

            JSONObject obj = response.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 error) { 
        Log.d(TAG, "Error: " + error.getMessage()); 
       } 
      }) 
      { 
       @Override 
       protected Map<String, String> getParams() 
       { 
        Map<String, String> params = new HashMap<String, String>(); 
        params.put("from", textfrom_btn); 
        params.put("to", textto_btn); 
        params.put("seat", totalpassenger); 

        return params; 
       } 
      }; 

      // Adding request to request queue 
      MyApplication.getInstance().addToRequestQueue(taxiReq); 

hier ist die json:

[ 
    { 
     "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 
    } 
    ] 

hier mein PHP-Code in meinem Server:

<?php 
include './include/DbHandler.php'; 
$db = new DbHandler(); 

// json response array 
$response = array(); 

if (isset($_POST['from']) && isset($_POST['to']) && isset($_POST['totalpassenger'])) { 

    // receiving the post params 
    $from = $_POST['from']; 
    $to = $_POST['to']; 
    $totalpassenger = $_POST['totalpassenger']; 

    // get the user by email and password 
    $taxilist = $db->getTaxi($from, $to, $totalpassenger); 

    if ($taxilist != false) { 
     // taxi is found 
     foreach($taxilist as $data){ 
      $response[] = array(
       "image" => "http://".$_SERVER['SERVER_NAME'].dirname($_SERVER['REQUEST_URI']).$data["image"], 
       "taxiname" => $data["taxiname"], 
       "from" => $data["from"], 
       "to" => $data["to"], 
       "departure" => $data["departure"], 
       "arrive" => $data["arrive"], 
       "seat" => $data["seat"], 
       "cost" => $data["cost"] 
      ); 
     } 
     echo json_encode($response); 
    } else { 
     $response["error_msg"] = "Sorry taxi is not listed"; 
     echo json_encode($response); 
    } 
} else { 
    $response["error_msg"] = "please choose city to view taxi list"; 
    echo json_encode($response); 
} 
?> 

ist es eine falsche Syntax in Code?

EDIT hier ist die logcat nachdem ich CustomJsonRequest

verwendet
03-29 16:11:06.070 25258-25258/com.testing W/EGL_genymotion: eglSurfaceAttrib not implemented 
03-29 16:11:06.078 25258-25258/com.testing D/OpenGLRenderer: TextureCache::get: create texture(0xb85fe538): name, size, mSize = 389, 5184, 173360 
03-29 16:11:06.082 25258-25258/com.testing D/OpenGLRenderer: TextureCache::get: create texture(0xb8489018): name, size, mSize = 391, 19264, 192624 
03-29 16:11:55.714 25258-25258/com.testing D/Searching: [] 
+0

, wie Sie die Post-Anforderung machen, wenn Sie die oben json Antwort erhalten?Bitte erwähnen Sie die Post-Variablen –

+0

sie sind textfrom_btn = "PTK", textto_btn = "SGU", totalpassenger = 2 ... das sind die Variablen, die ich versucht habe, an meine PHP übergeben ... sind diese was meinen Sie mit Post-Variablen ? – meeftah

+0

können Sie den vollständigen Code Ihres Servers anzeigen? Ihr JsonArrayRequest scheint gültig zu sein – HendraWD

Antwort

1

Day, bevor ich das gleiche Problem konfrontiert hatte. Es ist nicht das Problem mit Ihrem Code. Eigentlich JsonArrayRequest Klasse erweitert JsonRequest, die erweitert Anfrage. In JsonRequest Klasse getBody() Methode wurde overrided und es sieht so

@Override 
public byte[] getBody() { 
    try { 
     return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET); 
    } catch (UnsupportedEncodingException uee) { 
     VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", 
       mRequestBody, PROTOCOL_CHARSET); 
     return null; 
    } 
} 

Wenn Sie genau getParams() Methode aussehen wird nicht aufgerufen.

Also, was ich tat, war eine new CLASS zu schaffen, die Request<JsonArrayRequest> wie diese

erstreckt können Sie überschreiben die getParams() Methode in dieser Klasse eine Verwendung dieses für Ihre Anfrage so.

CustomJsonRequest request = new CustomJsonRequest(Request.Method.POST, url, params, 
     new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONArray response) { 

      } 
     }, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 

    } 
}); 

Ich hoffe, das ist hilfreich. Danke

+0

Ich werde versuchen ... Ich hoffe, es funktioniert – meeftah

+0

Es hat für mich gearbeitet! : D – Jois

+0

es gibt mir "D/Searching: []" auf Logcat – meeftah

0

Ich hatte dieses Problem letzte Woche und es hatte etwas damit zu tun, dass Volley die POST-Parameter nicht richtig einstellt. Anstatt es zu reparieren, ging ich einfach mit einer GET-Anfrage und es sieht so aus, als könnten Sie das Gleiche tun, da die Parameter nicht so sehr empfindlich zu sein scheinen.

Wenn Sie eine GET-Anfrage versuchen möchten, könnten Sie dies tun. Natürlich müssen Sie auch in PHP von POST auf GET umsteigen!

Die URL wird wie folgt erstellt:

URL url = null; 
String query = "?from=" + textfrom_btn + 
    "&to="  + textto_btn + 
    "&seat=" + totalpassenger; 
try { 
    url = new URL("http", "www.Foo.com", "Bar.php" + query); 
}catch (MalformedURLException ex){ 
    ex.printStackTrace(); 
} 

Anfrage sieht wie folgt aus:

JsonArrayRequest taxiReq = new JsonArrayRequest(Request.Method.GET, url.toString(), null, new Response.Listener<JSONArray>(){ 
    @Override 
    public void onResponse(JSONArray response){ 
     for(int i = 0; i < response.length(); i++){ 
      try{ 
       JSONObject obj = response.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(); 
      } 
     } 
     adapter.notifyDataSetChanged(); 


    } 
}, new Response.ErrorListener(){ 
    @Override 
    public void onErrorResponse(VolleyError error){ 
     error.printStackTrace(); 
    } 
}); 
MyApplication.getInstance().addToRequestQueue(taxiReq); 

} 
+0

posten Ich möchte nicht GET-Anfrage verwenden, ist anfällig für Injektion und Sicherheit – meeftah

+1

Völlig verständlich! Ich werde versuchen, die von @Jois bereitgestellte Lösung auch in meinem Projekt zu verwenden. – Carl