链接方式:
1)将应用后台服务器部署到某台可以通过公网访问的服务器上,手机访问该公网IP地址,类似于公网试用
(2)设置局域网,手机通过WIFI访问局域网中的某台服务器(拥有局域网IP地址)选择这种方案
下面主要讲android这边的开发
- 页面部分的按钮触发事件处理逻辑
/**
* 按钮触发下载
*
* @param view
* @throws MalformedURLException
*/
public void startDownImage(View view) throws MalformedURLException {
String path = urlEditText.getText().toString();// 获取图片路径字符串
Toast.makeText(getApplicationContext(), "开始下载图片", Toast.LENGTH_LONG)
.show();
Toast.makeText(
getApplicationContext(),
"IP: " + this.getLocalIpAddress() + " MAC: "
+ this.getLocalMacAddress(), Toast.LENGTH_LONG).show();
Log.i(TAG, "IP: " + this.getLocalIpAddress());
Log.i(TAG, "MAC: " + this.getLocalMacAddress());
// 下载图片,新建一个异步任务下载图片
ImageService.getImage(showImage, path);
}
/**
* 获取Android本机IP地址
*
* @return
*/
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("WifiPreference IpAddress", ex.toString());
}