我正在尝试向服务器发送消息并获得响应。当我尝试打开套接字时,出现异常:
我在 AndroidManifest.xml 中添加了以下几行:
< uses-permission android:name="android.permission.INTERNET" />
< uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在应用程序标签之前。
这是我的代码:
String IP = "81.218.150.49";
int port = 32001;
Socket my_socket = new Socket(IP, port); //Here i get exception !!
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(my_socket.getOutputStream(), "UTF8"));
wr.write("mobiwize server login test testpswd\r\n");
// Send data
wr.flush();
// Get response
StringBuffer sb = new StringBuffer("");
BufferedReader rd = new BufferedReader(new InputStreamReader(my_socket.getInputStream()));
String line ="";
String NL = System.getProperty("line.separator");
while ((line = rd.readLine()) != null) {
sb.append(line + NL);
}
httpStuff.setText(sb);
rd.close();
wr.close();
} catch (Exception e) {
}
我真的很感谢您的帮助!