2017-08-24 60 views
1

讓我們從我使用的代碼開始,嘗試了每一種可能的不同方式來製作「params」。我用它作爲一個HashMap,以Json格式,也作爲一個字符串。我也試圖通過創建一個hashmap並返回它覆蓋getParams()方法。沒有工作。

這是我調用JsonObjectRequest的函數。

private void sendJsonObjReq() { 

    showProgressDialog(); 
    Map<String, String> para = new HashMap<>(); 

    para.put("Condicao", "dea"); 
    para.put("Field", "1550"); 
    JSONObject jason = new JSONObject(para); 

    String params = jason.toString(); 
    textView.setText(params); 

     JsonObjectRequest jsonReq = new JsonObjectRequest(JsonRequest.Method.POST, 
       url_cond1, params, 
       new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
         Log.d(AppController.TAG, response.toString()); 
         textView.setText(response.toString()); 

        /*try { 
         int u = response.getInt("sucess"); 
         if(u == 1){ 

          JSONArray json = response.optJSONArray("Type"); 
          if(json != null) { 
           MakeListHashMap(json); 
          }else{ 
           textView.setText("Wrong Parameters"); 
          } 
         }else{textView.setText("success is 0");} 
        }catch(JSONException e){ 
         Log.e("Error:", e.getMessage()); 
         textView.setText("nothing here"); 
        }*/ 
         hideProgressDialog(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(AppController.TAG, "Error:" + error.getMessage()); 
       showProgressDialog(); 
      } 
     }); 
     //Add to request queue 
    AppController.getInstance().addToRequestQueue(jsonReq, tag_json_obj); 

} 

的URL,一切是好的,我檢查了,但我就是不明白,爲什麼無論是GET或POST方法的工作,因爲我已經嘗試過兩種,但我沒有與他們有任何的成功。我的php代碼包括:

<?php 
    $response = array(); 
//$oi = json_decode($_POST); 
//$response["Condicao"] = $_GET["Condicao"]; 
//$response["Field"] = $_GET["Field"]; 
    $response["Condicao"] = $_POST["Condicao"]; 
    $response["Field"] = $_POST["Field"]; 

    $response['sucess'] = 1; 
    $response['other'] = "test"; 

    echo json_encode($response); 
?> 

我試着解碼它,而不是解碼它,我真的很遺憾該怎麼做。我認爲這可能是服務器的問題,但使用應用程序「郵差」我可以發送GET和POST,響應是像我想要的Json數組。

如果我發送 key1 = Condicao => name = dea; key2 = Field => name = 1550;

我得到的結果

{ 
    "Condicao": "dea", 
    "Field": "1550", 
    "sucess": 1, 
    "other": "test" 
} 

編輯:解決的辦法是:

<?php 
    $_POST = json_decode(file_get_contents('php://input'), true); 
    $response = array(); 

    $response["Condicao"] = $_POST["Condicao"]; 
    $response["Field"] = $_POST["Field"]; 

    $response['sucess'] = 1; 
    $response['other'] = "test"; 

    echo json_encode($response); 
?> 
+0

如果您想使用$ _ POST數組,請不要發送json參數。 – greenapps

+0

https://stackoverflow.com/questions/18866571/receive-json-post-with-php –

+0

@Pavneet_Singh我已經閱讀了這個問題,但由於我使用的是一個android應用程序,我的目標是發送數據來填充數據庫,我無法使用從文件讀取的方法。 我缺乏知識是一個巨大的恥辱,你在哪裏,正確和感謝,那就是解決方案。 –

回答

1

1)傳遞,而不是字符串的JSON對象

JsonObjectRequest jsonReq = new JsonObjectRequest(JsonRequest.Method.POST, 
       url_cond1, jason , 
       //   ^^^^ 
       new Response.Listener<JSONObject>() { 

2)收到在您的PHP爲

<?php 
    $response = array(); 
    // receive your json object 
    $jasonarray = json_decode(file_get_contents('php://input'),true); 
    $response["Condicao"] = $jasonarray["Condicao"]; 
    $response["Field"] = $jasonarray["Field"]; 
    $response['sucess'] = 1; 
    $response['other'] = "test"; 

    echo json_encode($response); 
?>