与Android(客户端)和Node.js(服务器)的异步通信

与Android(客户端)和Node.js(服务器)的异步通信,android,node.js,cross-domain,Android,Node.js,Cross Domain,我想与Android和Node.js进行通信 我实施了这个程序,当他们在同一个网络上(我指的是同一个WIFI)时,效果很好。但当我断开android手机与wifi的连接并连接到移动数据时,通信就无法进行了。搜索后,我发现存在跨域问题。 但我找不到适合我情况的解决方案。 你能帮我解决这个问题吗 这是我的服务器端代码(server.js) var https=require('https'); var express=需要(“express”); var fs=需要('fs'); var-app=e

我想与Android和Node.js进行通信

我实施了这个程序,当他们在同一个网络上(我指的是同一个WIFI)时,效果很好。但当我断开android手机与wifi的连接并连接到移动数据时,通信就无法进行了。搜索后,我发现存在跨域问题。 但我找不到适合我情况的解决方案。 你能帮我解决这个问题吗

这是我的服务器端代码(server.js)

var https=require('https');
var express=需要(“express”);
var fs=需要('fs');
var-app=express();
var端口=9000;
var cors=要求(“cors”);
var welcome=“来自服务器的新消息!”;
app.get('/get',函数(req,res){
log('>>向客户端发送新消息…);
res.json(欢迎);
})
app.post('/post',函数(请求、回复){

console.log(“要解决此问题,您需要使用SSL证书或安全https使Node.js服务器处于活动状态。对于本地测试,您应该只使用相同的本地网络进行尝试。 尝试更新请求中的以下参数,看看是否有效

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS
Access-Control-Allow-Headers: Content-Type

感谢您的回复,有没有办法通过使用jsonp或代理来解决这个问题??
public class SendToServer extends AsyncTask<String, String, String> {

private TextView tv;
private int action;
private int get = 1;
private int post = 2;


public SendToServer (TextView tv, int action) {
    this.tv = tv;
    this.action = action;
}

public String get_action(HttpURLConnection connection) {

    InputStream stream;
    BufferedReader reader = null;
    StringBuffer buffer;

    try {
        connection.connect();

        stream = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(stream));
        buffer = new StringBuffer();
        String line = "";

        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }

        return buffer.toString();
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        if (connection != null) {
            connection.disconnect();
        }
        try {

            if (reader != null) {
                reader.close();
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }//end of finally

    return null;

}
public String post_action(HttpURLConnection connection) {

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.accumulate("user_name", "Na Jun Yeop");
        jsonObject.accumulate("finger_print", "12388452218");
    } catch (JSONException e) {
        e.printStackTrace();
    }


    OutputStream outputStream;
    InputStream inputStream;
    BufferedWriter writer = null;
    BufferedReader reader = null;
    StringBuffer buffer;

    try {
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "text/html");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.connect();

        outputStream = connection.getOutputStream();
        writer = new BufferedWriter(new OutputStreamWriter(outputStream));

        writer.write(jsonObject.toString());
        writer.flush();
        writer.close();

        inputStream = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(inputStream));
        buffer = new StringBuffer();
        String line = "";

        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }

        return buffer.toString();
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        if (connection != null) {
            connection.disconnect();
        }
        try {

            if (reader != null) {
                reader.close();
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }//end of finally

    return null;
}

@Override
protected String doInBackground(String... strings) {

    HttpURLConnection connection = null;
    BufferedReader reader = null;

    try {
        URL url = new URL(strings[0]);
        connection = (HttpURLConnection)url.openConnection();

        if (action == get) {
            return get_action(connection);
        }
        else if (action == post) {
            return post_action(connection);
        }

        connection.connect();

        InputStream stream = connection.getInputStream();

        reader = new BufferedReader(new InputStreamReader(stream));

        StringBuffer buffer = new StringBuffer();

        String line = "";

        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }

        return buffer.toString();


    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        if (connection != null) {
            connection.disconnect();
        }
        try {

            if (reader != null) {
                reader.close();
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }//end of finally


    return null;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    tv.setText(result);
}
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS
Access-Control-Allow-Headers: Content-Type