如何连接到特定的IP和端口在android
问题描述:
答
URLConnection connection = null;
URL url = null;
InputStream is = null;
String mUrl="IP ADDRESS AND PORT NUMBER";
try {
url = new URL(mUrl);
connection = url.openConnection();
connection.setRequestProperty("Accept", "SPECIFY ACCEPT TYPE");
is = connection.getInputStream();
String data=convertStreamToString(is);
} catch (Exception e) {
Log.i("Exception while reading inputstream From URL :"+mUrl, e.getMessage());
e.printStackTrace();
}
+1
这更适合通过HTTP请求连接到服务器**不是用于套接字通信 – iTech 2013-02-14 11:42:22
的可能重复的[实施例:使用的AsyncTask的Android双向网络套接字(http://*.com/questions/5135438/example-android-bi-directional-network-socket-using-asynctask) – SztupY 2013-02-15 11:20:28