0

我有一个大问题。我无法从 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)

我需要更多权限吗?

4

2 回答 2

1

127.0.0.1 是模拟器的ip。10.0.2.2 是您正在寻找的(运行模拟器的主机)

于 2013-05-02T21:47:29.067 回答
0

首先确保您在清单文件中放入互联网权限,然后确保您的主机和端口号都正确,最后检查您是否有权访问该主机+端口。

于 2013-05-02T22:04:07.923 回答