我有一个大问题。我无法从android模拟器连接到Java服务器套接字。我有日志和代码:
我正在使用2.1版(Eclair)模拟器来运行我的Android应用程序。
System.out.println("Sup");
try{
Socket socket = new Socket("127.0.0.1", 26753);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("e:ask");
out.println("p:pass");
out.println("EOF");
socket.close();
} catch (Exception e){
e.printStackTrace();
}
Logcat错误:
05-02 21:35:41.986: I/System.out(276): Sup
05-02 21:35:41.996: W/System.err(276): java.net.ConnectException: /127.0.0.1:26753 - Connection refused
05-02 21:35:41.996: W/System.err(276): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:255)
05-02 21:35:41.996: W/System.err(276): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:220)
05-02 21:35:41.996: W/System.err(276): at java.net.Socket.startupSocket(Socket.java:780)
05-02 21:35:41.996: W/System.err(276): at java.net.Socket.tryAllAddresses(Socket.java:192)
05-02 21:35:42.006: W/System.err(276): at java.net.Socket.<init>(Socket.java:256)
05-02 21:35:42.006: W/System.err(276): at java.net.Socket.<init>(Socket.java:220)
05-02 21:35:42.006: W/System.err(276): at TestClass.skytheventure.TestClass.onClick(TestClass.java:38)
05-02 21:35:42.006: W/System.err(276): at android.view.View.performClick(View.java:2364)
05-02 21:35:42.006: W/System.err(276): at android.view.View.onTouchEvent(View.java:4179)
05-02 21:35:42.006: W/System.err(276): at android.widget.TextView.onTouchEvent(TextView.java:6541)
05-02 21:35:42.006: W/System.err(276): at android.view.View.dispatchTouchEvent(View.java:3709)
05-02 21:35:42.006: W/System.err(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-02 21:35:42.006: W/System.err(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-02 21:35:42.006: W/System.err(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-02 21:35:42.016: W/System.err(276): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-02 21:35:42.016: W/System.err(276): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
05-02 21:35:42.016: W/System.err(276): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
05-02 21:35:42.016: W/System.err(276): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
05-02 21:35:42.016: W/System.err(276): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
05-02 21:35:42.016: W/System.err(276): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
05-02 21:35:42.016: W/System.err(276): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 21:35:42.016: W/System.err(276): at android.os.Looper.loop(Looper.java:123)
05-02 21:35:42.016: W/System.err(276): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-02 21:35:42.016: W/System.err(276): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 21:35:42.027: W/System.err(276): at java.lang.reflect.Method.invoke(Method.java:521)
05-02 21:35:42.027: W/System.err(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-02 21:35:42.027: W/System.err(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-02 21:35:42.027: W/System.err(276): at dalvik.system.NativeStart.main(Native Method)
我需要更多权限吗?
答案 0 :(得分:1)
127.0.0.1是模拟器的ip。 10.0.2.2是您正在寻找的(模拟器运行的主机)
答案 1 :(得分:0)
首先确保你在你的清单文件中添加了Internet权限,然后确保你的主机和端口号都正确,最后检查你是否有权访问该主机+端口。