Android 如何将安卓连接到服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2696190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:59:29 来源:igfitidea点击:
How to connect android to server
提问by Sijith
How to connect android to server(PC) and pass values to it
如何将android连接到服务器(PC)并将值传递给它
回答by Paul Gregtheitroade
Here is a means for sending an "id" and "name" to a server:
这是一种向服务器发送“id”和“name”的方法:
URL url = null;
try {
String registrationUrl = String.format("http://myserver/register?id=%s&name=%s", myId, URLEncoder.encode(myName,"UTF-8"));
url = new URL(registrationUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Log.d("MyApp", "Registration success");
} else {
Log.w("MyApp", "Registration failed for: " + registrationUrl);
}
} catch (Exception ex) {
ex.printStackTrace();
}
回答by Erich Douglass
Take a look at HttpClientif you want to connect to an HTTP server, or use raw sockets if you want to roll your own protocol.
如果要连接到 HTTP 服务器,请查看HttpClient,如果要滚动自己的协议,请使用原始套接字。