0

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...

1 Answer 1

1

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~

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.