$\ php中的POST未使用Volley库JsonObjectRequest(方法POST)填充
让我们从我正在使用的代码开始,我尝试了每一种可能的不同方法来生成“params”。我将其用作HashMap、Json格式以及字符串。我还试图通过创建一个hashmap并返回它来@Override getParams()方法。什么都没用 下面是调用JsonObjectRequest的函数$\ php中的POST未使用Volley库JsonObjectRequest(方法POST)填充,php,android,json,android-volley,Php,Android,Json,Android Volley,让我们从我正在使用的代码开始,我尝试了每一种可能的不同方法来生成“params”。我将其用作HashMap、Json格式以及字符串。我还试图通过创建一个hashmap并返回它来@Override getParams()方法。什么都没用 下面是调用JsonObjectRequest的函数 private void sendJsonObjReq() { showProgressDialog(); Map<String, String> para = new HashM
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);
}
编辑:解决方案是:
<?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);
?>
1.)传递json对象而不是字符串
JsonObjectRequest jsonReq = new JsonObjectRequest(JsonRequest.Method.POST,
url_cond1, jason ,
// ^^^^
new Response.Listener<JSONObject>() {
JsonObjectRequest-jsonReq=新的JsonObjectRequest(JsonRequest.Method.POST,
第1条,杰森,
// ^^^^
新的Response.Listener(){
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);
?>
如果你想使用$\uPost数组,请不要发送json参数。@Pavneet\u Singh我已经读过这个问题,但因为我使用的是android应用程序,我的目标是发送数据来填充数据库,所以我不能使用从文件读取的方法。我缺乏知识是一个巨大的耻辱,你说得对,谢谢,这是解决方法N
JsonObjectRequest jsonReq = new JsonObjectRequest(JsonRequest.Method.POST,
url_cond1, jason ,
// ^^^^
new Response.Listener<JSONObject>() {
<?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);
?>