我正在尝试发送包含 2 个字段(用户和通行证)的帖子消息。
处理帖子的node.js(只是打印代码,不是所有功能):
app.post('/login.html', function (req, res) {
log.info("Got: " , req.body);
userName = req.body.name;
pass = req.body.pass;
}
Android 应用程序看起来:
Network network = new BasicNetwork(new HurlStack());
// Instantiate the RequestQueue with the cache and network.
mRequestQueue = new RequestQueue(cache, network);
// Start the queue
mRequestQueue.start();
JsonPost(mRequestQueue);
JsonPost 方法看起来:
String url = getResources().getString(R.string.json_get_url);
Map<String, String> jsonParams = new LinkedHashMap<String, String>();
try {
// adding some keys
jsonParams.put("name", "root");
jsonParams.put("pass", "123456");
} catch (Exception ex) {
ex.printStackTrace();
}
// Initialize a new JsonObjectRequest instance
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.POST, url, new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.toString());
}
}
)
我收到 {}
(node.js 中的空 json 输出:GOT:{})
我看了这篇文章:(但没有找到答案) Getting Empty JSON response
为什么我得到空的 json 正文,如何修复它?
最佳答案
也许您发布的代码不完整,但我们应该使用以下行:
mRequestQueue.add(jsonObjectRequest);
所以请检查一下。
关于android - 为什么我得到空的 json 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52473744/