2016-03-29 14 views
1

内の任意のパラメータを渡していない私は自分のサーバーにポストパラメータからエラーを得た、その私のparamsなどは全く私のサーバーに渡していない...私が得たすべては、次のとおりです。バレーボール、JsonArrayRequest

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

ことメッセージは、ここでしか私のサーバーサイドにように[ 'から'] ISSET($ _ POST場合

を出てくる私のコードです:ここ

​​

は、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 
    } 
    ] 

ここに私のPHPコード:

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

コード内の任意の間違った構文がありますか?

EDIT は、ここで私はCustomJsonRequest

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

はどのようにPOSTリクエストを作っていますか?ポスト変数 –

+0

を挙げてください。それらはtextfrom_btn = "PTK"、textto_btn = "SGU"、totalpassenger = 2 ...これらは私のPHPに渡そうとした変数です...これらはポスト変数? – meeftah

+0

あなたはサーバーの完全なコードを表示できますか?あなたのJsonArrayRequestは有効です – HendraWD

答えて

1

を使用した後の日の私は、同じ問題に直面していた前に、logcatです。それはあなたのコードに問題ではありません。実際にJsonArrayRequestクラスはを継承しています。です。です。 getBody()クラスJsonRequest方法がオーバーライドされていて、あなたがgetParams()メソッドが呼び出されていないよく見るとそれはそう

@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; 
    } 
} 

を探します。

だから何私がやったことはあなたがのgetParams()このクラスの方法このようなあなたの要求のために使用これを上書きすることができますRequest<JsonArrayRequest>

このような

を拡張new CLASSを作成することでした。

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) { 

    } 
}); 

私はこれが役に立ちそうです。ありがとう

+0

私は試してみる...私はそれがうまくいきます。 – meeftah

+0

それは私のために働いた! :D – Jois

+0

それは私に "D /検索:[]" logcat – meeftah

0

私は先週、この問題を抱えていました。これは、VolleyがPOSTパラメータを正しい方法で設定していないことと関連していました。それを修正するのではなく、ちょうどGET要求に行きました、そして、パラメータが非常に敏感でないようにあなたが同じことをすることができるように見えます。

GETリクエストを試したい場合は、このようにすることができます。もちろん、PHPでもPOSTからGETに切り替える必要があります!

URLは次のように作成されます。

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

要求は次のようになります。あなたは上記のJSONレスポンスを得たとき

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

私はGETリクエストを使用したくありません。注射とセキュリティに脆弱です – meeftah

+1

完全に理解できます!私は@Joisによって提供されたソリューションを私のプロジェクトにも使用しようとします。 – Carl

関連する問題