2017-08-24 58 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); 
?>