Android Volley POST正文请求在后端收到了EMPTY

时间:2015-04-20 07:05:45

标签: json post android-volley

我正在使用以下代码将json对象发布到php服务器:

    Map<String, String> paramsMap = new HashMap<String, String>();
    paramsMap.put("tag", "jsonParams");
    JSONObject jsonObject = new JSONObject(paramsMap);
    Log.d("JSON", jsonObject.toString());
    JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("JSON RESPONSE", response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("JSON ERROR", error.getMessage());
                }
            });


    requestQ.add(jsonRequest);

并使用它来接收php中的对象:

    $body = '';
    $handle = fopen('php://input','r');
    while(!feof($handle)){
        $body .= fread($handle,1024);
    }
    $logger->log("login request","request body: ".$body);

问题是$ body总是空的我使用FIDDLER来检查我的HTTP请求,它就像这样的原始数据:{“tag”:“jsonParams”} 那我搞砸了什么? thx提前。

2 个答案:

答案 0 :(得分:0)

我知道这是一个老问题,但对于未来的读者......

我使用StringRequest代替JsonObjectRequest解决了完全相同的问题。构造函数略有不同,然后您可以轻松地解析对JsonObject的字符串响应,如下所示:

JSONObject response = new JSONObject(responseString);

答案 1 :(得分:0)

不确定你的问题是什么,但未来的googlers:

我的问题是我没有从php://input

阅读

完整代码(正常工作):

爪哇:

JSONObject jsonobj; // declared locally so that it destroys after serving its purpose
jsonobj = new JSONObject();
try {
    // adding some keys
    jsonobj.put("new key", Math.random());
    jsonobj.put("weburl", "hashincludetechnology.com");
    // lets add some headers (nested JSON object)
    JSONObject header = new JSONObject();
    header.put("devicemodel", android.os.Build.MODEL); // Device model
    header.put("deviceVersion", android.os.Build.VERSION.RELEASE); // Device OS version
    header.put("language", Locale.getDefault().getISO3Language()); // Language
    jsonobj.put("header", header);
    // Display the contents of the JSON objects
    display.setText(jsonobj.toString(2));
} catch (JSONException ex) {
    display.setText("Error Occurred while building JSON");
    ex.printStackTrace();
}

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonobj, new Response.Listener<JSONObject>() {


    @Override
    public void onResponse(JSONObject response) {
        System.out.println("onResponse()");

        try {
            result.setText("Response: " + response.toString(2))

            System.out.println("Response: " + response.toString(2));
        } catch (JSONException e) {
            display.setText("Error Occurred while building JSON");
            e.printStackTrace();
        }
        //to make sure it works backwards as well

    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        System.out.println("onErrorResponse()");
        System.out.println(error.toString());


    }
});


System.out.println("After the request is made");
// Add the request to the RequestQueue.
queue.add(jsObjRequest);

澄清:displayresult是我用来在屏幕上显示数据的两个TextView个对象,而queue是Volley的请求队列。< / p>

PHP:

$inp = json_decode(file_get_contents('php://input')); //$input now contains the jsonobj
echo json_encode(["foo"=>"bar","input"=>$inp]); //to make sure we received the json and to test the response handling

您的Android Monitor应输出某事。喜欢:

{
    "foo":"bar",
    "input":{
        "new key":0.8523024722406781,
        "weburl":"hashincludetechnology.com",
        "header": {
            "devicemodel":"Android SDK built for x86",
            "deviceVersion":"7.1",
            "language":"eng"
        }
    }
}