Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从android应用程序调用节点服务器的路由_Android_Node.js - Fatal编程技术网

如何从android应用程序调用节点服务器的路由

如何从android应用程序调用节点服务器的路由,android,node.js,Android,Node.js,在localhost上运行,我的手机也连接到了同一个主机 网络可以将我的android应用程序与running连接起来吗 节点服务器。我要做的是调用一个只有 在服务器的console.log中打印“Hello world”。 如果可能的话,请帮我写代码。 谢谢 **这是我在android中的ooClick功能** 公共无效发送消息(查看){ name=(EditText)findViewById(R.id.name); email=(EditText)findviewbyd(R.id.email

在localhost上运行,我的手机也连接到了同一个主机 网络可以将我的android应用程序与running连接起来吗 节点服务器。我要做的是调用一个只有 在服务器的console.log中打印“Hello world”。 如果可能的话,请帮我写代码。 谢谢

**这是我在android中的ooClick功能**
公共无效发送消息(查看){
name=(EditText)findViewById(R.id.name);
email=(EditText)findviewbyd(R.id.email);
phone=(EditText)findViewById(R.id.phone);
地址=(EditText)findViewById(R.id.address);
试一试{
字符串nme=URLEncoder.encode(name.getText().toString(),“UTF-8”);
字符串eMail=urlcoder.encode(eMail.getText().toString(),“UTF-8”);
字符串ph=URLEncoder.encode(phone.getText().toString(),“UTF-8”);
字符串add=URLEncoder.encode(address.getText().toString(),“UTF-8”);
//创建http客户端对象以向服务器发送请求
HttpClient=new DefaultHttpClient();
//创建URL字符串
字符串URL=”http://192.168.0.183:3000/save?text1=“+nme+”&text2=“+eMail+”&text3=“+ph+”&text4=“+add;
//Toast.makeText(getBaseContext(),“请稍候,连接到服务器。”+URL,Toast.LENGTH_LONG.show();
尝试
{
字符串SetServerString=“”;
//创建对服务器的请求并获取响应
HttpGet HttpGet=新的HttpGet(URL);
ResponseHandler ResponseHandler=新BasicResponseHandler();
SetServerString=Client.execute(httpget,responseHandler);
//显示对活动的响应
//content.setText(SetServerString);
Toast.makeText(getBaseContext(),“response”+SetServerString,Toast.LENGTH_LONG.show();
}
捕获(例外情况除外)
{
Toast.makeText(getBaseContext(),“fail”,Toast.LENGTH_LONG.show();
}
}捕获(不支持的编码异常e){
//TODO自动生成的捕捉块
Toast.makeText(getBaseContext(),“response”,Toast.LENGTH_LONG.show();
}
**这是我在节点服务器中的函数**
app.get('/save',函数(req,res){
var text1=请求参数('text1');
var text2=请求参数('text2');
var text3=请求参数('text3');
var text4=请求参数('text4');
console.log(text1+''+text2+''+text3);
});

我只想在控制台中打印文本。

您应该研究改型库的使用。这使得从和Android应用程序进行HTTP调用非常容易。此外,您还需要外部IP地址或服务器名称,以便通过网络访问数据。“localhost”始终指软件运行的当前计算机。对于桌面,“localhost”是桌面;对于Android设备,“localhost”是Android设备。

您需要服务器的ip地址或url,而不是“localhost”.ya我正在提供url ip地址,但仍然在控制台上。我可以从浏览器访问该页面。什么控制台?你在使用什么代码?我已经给出了我在java和节点服务器中使用的代码。你能在这方面帮助我吗?
I am working on android for the first time, I just want to connect my android app with node server on a button click. my node server is
**This is my ooClick function in android.**
public void sendMessage(View view) {

         name     =  (EditText)findViewById(R.id.name);
         email      =  (EditText)findViewById(R.id.email);
         phone       =  (EditText)findViewById(R.id.phone);
         address       =  (EditText)findViewById(R.id.address);


         try {
             String nme    = URLEncoder.encode(name.getText().toString(), "UTF-8");
             String eMail  = URLEncoder.encode(email.getText().toString(), "UTF-8");
             String ph   = URLEncoder.encode(phone.getText().toString(), "UTF-8");
            String add    = URLEncoder.encode(address.getText().toString(), "UTF-8");



            // Create http client object to send request to server
            HttpClient Client = new DefaultHttpClient();

            // Create URL string

            String URL = "http://192.168.0.183:3000/save?text1="+nme+"&text2="+eMail+"&text3="+ph+"&text4="+add;
            //Toast.makeText(getBaseContext(),"Please wait, connecting to server."+URL,Toast.LENGTH_LONG).show();
            try
            {
                          String SetServerString = "";

                        // Create Request to server and get response

                          HttpGet httpget = new HttpGet(URL);
                         ResponseHandler<String> responseHandler = new BasicResponseHandler();
                         SetServerString = Client.execute(httpget, responseHandler);

                          // Show response on activity 

                         //content.setText(SetServerString);
                         Toast.makeText(getBaseContext(),"response"+SetServerString,Toast.LENGTH_LONG).show();
             }
           catch(Exception ex)
              {
               Toast.makeText(getBaseContext(),"fail",Toast.LENGTH_LONG).show();
               }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            Toast.makeText(getBaseContext(),"response",Toast.LENGTH_LONG).show();
        }


**This is my function in node server**


app.get('/save', function(req, res){

        var text1= req.param('text1');
        var text2= req.param('text2');
        var text3= req.param('text3');
        var text4= req.param('text4');
console.log(text1+' '+text2+' '+text3);

});