当前位置: 首页 > 知识库问答 >
问题:

后端收到的Android凌空邮件正文请求为空

花阳辉
2023-03-14

我使用以下代码将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个答案

吕征
2023-03-14

不确定你的问题是什么,但对于未来的谷歌用户来说:

我的问题是我没有阅读<代码>php://input

完整代码(工作):

Java:

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

澄清:显示和结果是我用来在屏幕上显示数据的两个对象,队列是凌空的请求队列。

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显示器应该输出如下内容:

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

莫翰藻
2023-03-14

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

我通过使用StringRequest而不是JsonObjectRequest解决了完全相同的问题。构造函数差别很小,然后可以很容易地将字符串响应解析为JsonObject,如下所示:

JSONObject response = new JSONObject(responseString);
 类似资料:
  • 我正在我的本地主机上构建Web应用程序。 前端是Reactjs框架,运行在LocalHost:3000中

  • 我想在应用程序启动时加载一些数据,这些数据将被所有活动使用。我在应用程序的onCreate方法中这样做,因为我希望在显示任何活动之前下载这些数据,所以我尝试使用RequestFuture发出同步请求。但是,始终会抛出TimeoutException。 什么是获得应用程序范围的数据的最佳方式,记住没有一个活动是首先启动的?

  • 我正在尝试将文件从邮递员上传到节点。js服务器。我使用multer和body parse来解析请求的主体。 用例: 当我在Postman中使用带有raw-JSON作为Body的POST请求时,一切正常,我可以完全按照Postman中提供的那样看到req.body,也可以将新的键值对分配给req.body,以便这些新变量可以在路由中的进一步中间件中使用。 但是当我尝试从邮递员上传一个带有form-d

  • 我尝试发送一个删除请求到我的服务器,但我得到一个400坏的请求错误代码。我找了很多,但我找不到任何有帮助解决方案。当我尝试与邮差,请求工作良好。 这是来自Postman的curl命令的外观: 这是我的Java代码: 我还尝试在头中不设置Content-Type,但当我得到一个415错误时,我还删除了getBodyContentType()方法,但这也没有改变。 还有什么能帮上忙的点子吗?

  • 嗯,我是这个论坛的新手,如果你能在这方面帮助我,请。我搜索了一下,但找不到如何在截击请求中添加标题。我有这段代码,我想添加accept编码:gzip和api键。我会感谢你的帮助。代码如下:

  • 问题内容: 我在用JavaScript抓取API挣扎。当我尝试通过获取将某些内容发布到服务器时,请求正文为空数组。但是,当我使用Postman时,它可以工作…这是我在NodeJS中的服务器代码: 这是我的客户: 问题是在服务器端,req.body为空。有人能帮我吗?谢谢 ! 问题答案: 问题是 从文档中 … 防止该方法成为HEAD,GET或POST之外的任何其他内容, 并且 阻止 标头成为 简单标