繁体   English   中英

如何将Android http发布到Node.js

[英]How to Android http post to Node.js

我想从Android向Node.js服务器发送一个简单的HTTP POST请求。 谁能告诉我我在做什么错?

Android代码:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://54.68.139.250/bscreateuser");

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", "hellomoto"));
        nameValuePairs.add(new BasicNameValuePair("password", "pswd"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Node.js代码:

 app.get('/bscreateuser', function(request, response) { process.stdout.write("Attempted to create a user."); bscreateUser(request.query.username, request.query.password); response.send('-- BS -- User Created! username: ' + request.query.username + ' password: ' + request.query.password); }); function bscreateUser(username, password) { messageBody = 'create_user("' + username + '","' + password + '")'; queueUrl = DAO_QUEUE_URL; // sys.puts("--- going for BS ---"); sendSQSMessage(JSON.stringify(messageBody), queueUrl); } 

node.js代码中,您正在使用

app.get('/bscreateuser', ...

相反,您需要使用post方法

app.post('/bscreateuser', ...

并访问所需的变量,如下所示:

request.body.varname

您进行GET路由,如果要发布数据,则必须将其发布

节点代码

app.post(..

和参数可能会得到

request.body.username or request.body.password

暂无
暂无

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

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