简体   繁体   中英

android to node.js on http post

I send to nodejs in android

this ---

    try {
        URL postUrl = new URL("http://192.168.25.36:9001/");
        HttpURLConnection con = (HttpURLConnection)postUrl.openConnection();
        con.setDoOutput(true);
        con.setInstanceFollowRedirects(false);
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");

        OutputStream os = con.getOutputStream();
        os.write(jsonArray.toString().getBytes());
        os.flush();

        con.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }

I think it send successful

because when i send, my loger is give to me for success

my loger is trace this send function start and end.

when i wrong port on(search like 1234, 9002), its no send function end successful

but why my node.js server is no action?

my node.js server is

this ---

var http = require('http');


http.createServer(function(req, res){
    console.log('server start');
    if("POST" == req.method){
        console.log('POST');
    }
}).listen(SERVER_PORT);

i think when i start on node.js

i recv to "server start" on console

but no one word is recv

how is this possible?

i start node.js but no "server start" msg

but my android send function is normaly end

is this right?

i don't know what is this...

I'm so sorry

I success myself

my search is not enogh

I fix my code

android ---

        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(URL);

        StringEntity entity = new StringEntity(jsonArray.toString(), HTTP.UTF_8);
        post.setEntity(entity);
        post.setHeader("Accept", "application/json");
        post.setHeader("content-type", "application/json");

        HttpResponse response = (HttpResponse)client.execute(post);

node.js ---

var http = require('http');


var server = http.createServer(function(req, res){
    console.log('server start');
    if("POST" == req.method){
        console.log('POST');
        res.writeHead(200, {"Content-Type": "text/plain"});
        res.write("success");
        res.end();
    }
});

server.listen(SERVER_PORT); console.log("server run");

thanks~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM