Android 在node.js中解析为JSON
我有这个Java代码Android 在node.js中解析为JSON,android,json,http,node.js,post,Android,Json,Http,Node.js,Post,我有这个Java代码 DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpPostRequest = new HttpPost(URL); StringEntity se; se = new StringEntity(jsonObjSend.toString());
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se;
se = new StringEntity(jsonObjSend.toString());
// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
//httpPostRequest.setHeader("Accept-Encoding", "gzip");
// only set this parameter if you would like to use gzip compression
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
在node.js中
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
console.log("Entering");
if ( request.method === 'POST' ) {
// the body of the POST is JSON payload.
request.pipe(process.stdout);
}
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
我使用管道在控制台中写入所有内容,以确保收到数据。
我真正想要的是将数据解析回JSON,然后将其保存在数组中。
如何从请求中获取数据?
有人有代码示例吗
谢谢请尝试在代码中使用以下概念
response.on('data', function (chunk)
{
var data = chunk.toString();
var data_val = JSON.parse(data)
});
谢谢。。我有一个关于“request.on('data',function(chunk)…”行的最后一个问题,参数'data'是什么意思…它是在什么地方定义的?'data'和'end'是事件代码。请检查此文档
response.on('data', function (chunk)
{
var data = chunk.toString();
var data_val = JSON.parse(data)
});