简体   繁体   English

android到http上的node.js

[英]android to node.js on http post

I send to nodejs in android 我发送到Android中的nodejs

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 当我在端口上输入错误(搜索如1234、9002)时,其无发送功能成功结束

but why my node.js server is no action? 但是为什么我的node.js服务器没有动作?

my node.js server is 我的node.js服务器是

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 我认为当我开始使用node.js时

i recv to "server start" on console 我在控制台上转到“服务器启动”

but no one word is recv 但是没有一个词是revv

how is this possible? 这怎么可能?

i start node.js but no "server start" msg 我启动node.js,但没有“服务器启动”消息

but my android send function is normaly end 但是我的android发送功能正常结束

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 --- 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 --- 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); server.listen(SERVER_PORT); console.log("server run"); console.log(“服务器运行”);

thanks~ 谢谢〜

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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