首页 > 解决方案 > 我目前正在尝试创建一个可以将数据从浏览器发送到 android 应用程序的程序

问题描述

我目前正在尝试创建一个可以将数据从浏览器发送到 android 应用程序的程序,在这个程序中,我只能从浏览器到 Android Studio 虚拟机成功地做到这一点。但是我想将数据从浏览器发送到手机上的应用程序,我需要如何更改我的地址才能使其正常工作?

编辑:我是否必须使用相同的 wifi 才能使其工作?

 //<code>im</code> in address is the endpoint configured in server.
 //If you are using AVD provided by Android Studio, you should uncomment the upper address.
 //If you are using Genymotion, nothing else to do.



public class Const {
public static final String TAG = "xlui";
public static final String placeholder = "placeholder";
 
private static final String ADDRESS = "ws://10.0.2.2:8080/im/websocket";//for android vm
//public static final String address = "ws://10.0.3.2:8080/im/websocket";//for Genymotion

public static final String broadcast = "/broadcast";
public static final String broadcastResponse = "/b";
// replace {@code placeholder} with group id
public static final String group = "/group/" + placeholder;
public static final String groupResponse = "/g/" + placeholder;
public static final String chat = "/chat";
// replace {@code placeholder} with user id
public static final String chatResponse = "/user/" + placeholder + "/msg";
}

标签: javawebsocketokhttpstomp

解决方案


对于开发用途,我个人更喜欢本地解决方案。这意味着您使用与服务器相同的 WIFI 并使用该服务器的公共 IP 地址作为 IP。

例如,您的配置看起来像(在本地网络站点上)路由器运行在 10.0.0.1 服务器运行在 10.0.0.24 客户端运行在 10.0.0.50

所以网络套接字将运行"ws://10.0.0.24:8080/im/websocket"


推荐阅读