0

私はこのJavaコードを持っています

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 に戻し、それを配列に保存することです。リクエストからデータを取得するにはどうすればよいですか? 誰かがコード例を持っていますか?

ありがとう

4

2 に答える 2

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

        var data = '';
        request.on('data', function(chunk) {
            data += chunk;
        });

        request.on('end', function() {
            try {
                data = JSON.parse(data);
            } catch (e) {
                console.log(e);
            }
        });
    }

});

// 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/");
于 2012-08-08T10:08:44.227 に答える
0

コードで次の概念を使用してみてください

        response.on('data', function (chunk)
        {
                var data = chunk.toString();
                var data_val = JSON.parse(data)
          });
于 2013-08-29T07:56:48.393 に答える