是否有可能将数据从android发布到节点js?

时间:2014-02-24 06:33:12

标签: javascript android node.js post

我使用 node.js 作为服务器和android作为客户端,服务器正常工作从客户端发送和接收数据(除了android)

这里是我在javascript中的代码

function put(id, data, callback) {
    $.ajax('http://mydomain.com:8888/' + id + '/', {
        type: 'POST',
        data: JSON.stringify(data),
        dataType: 'json',
        success: function(data) {
            if (callback)
                callback(data);
        },
        error: function() {
            if (callback)
                callback(false);
        }
    });
}

和我的节点脚本

function handler ( req, res ) {
    if ( req.method === 'POST' ) {
       console.log('receive data from post');
    }
}

上面的代码已成功发送数据。

我想在android中发送数据(post)到节点(就像javascript那样)?

我是如何实现这一目标的?

感谢

1 个答案:

答案 0 :(得分:0)

但当然

Here you go

public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

  } catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
  } catch (IOException e) {
    // TODO Auto-generated catch block
  }
}