我正在尝试创建一个连接到桌面服务器的Android客户端应用程序 我成功地从笔记本电脑到服务器 我已将INTERNET和NETWORK权限放在清单文件中 程序在创建套接字时崩溃,eclipse debuuger将我转到socket.class源文件。
更新 这是我的完整代码
public class EsmFamilActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_esm_famil);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.esm_famil, menu);
return true;
}
//handler for sendtoserver
public void sendToServer(View view){
...
String host = "192.168.1.7";
int port = 8888;
try{
Socket s = new Socket(host, port);
Writer out = new OutputStreamWriter(s.getOutputStream());
out.write(data + "\n");
out.flush();
s.close();
}catch (UnknownHostException ex) {
ex.printStackTrace();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
这是我的logcat
01-18 21:43:06.908: D/TextView(1608): Constructor -- Got mEditTextBackgroundColor
01-18 21:43:09.658: W/IInputConnectionWrapper(1608): beginBatchEdit on inactive InputConnection
01-18 21:43:09.668: W/IInputConnectionWrapper(1608): endBatchEdit on inactive InputConnection
01-18 21:43:11.448: W/IInputConnectionWrapper(1608): beginBatchEdit on inactive InputConnection
01-18 21:43:11.448: W/IInputConnectionWrapper(1608): endBatchEdit on inactive InputConnection
01-18 21:43:46.018: D/AndroidRuntime(1608): Shutting down VM
01-18 21:43:46.018: W/dalvikvm(1608): threadid=1: thread exiting with uncaught exception (group=0x42f02500)
01-18 21:43:46.038: E/AndroidRuntime(1608): FATAL EXCEPTION: main
01-18 21:43:46.038: E/AndroidRuntime(1608): java.lang.IllegalStateException: Could not execute method of the activity
01-18 21:43:46.038: E/AndroidRuntime(1608): at android.view.View$1.onClick(View.java:3678)
01-18 21:43:46.038: E/AndroidRuntime(1608): at android.view.View.performClick(View.java:4300)
01-18 21:43:46.038: E/AndroidRuntime(1608): at android.view.View$PerformClick.run(View.java:18026)
01-18 21:43:46.038: E/AndroidRuntime(1608): at android.os.Handler.handleCallback(Handler.java:730)
01-18 21:43:46.038: E/AndroidRuntime(1608): at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 21:43:46.038: E/AndroidRuntime(1608): at android.os.Looper.loop(Looper.java:150)
01-18 21:43:46.038: E/AndroidRuntime(1608): at android.app.ActivityThread.main(ActivityThread.java:5162)
01-18 21:43:46.038: E/AndroidRuntime(1608): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 21:43:46.038: E/AndroidRuntime(1608): at java.lang.reflect.Method.invoke(Method.java:525)
01-18 21:43:46.038: E/AndroidRuntime(1608): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-18 21:43:46.038: E/AndroidRuntime(1608): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-18 21:43:46.038: E/AndroidRuntime(1608): at dalvik.system.NativeStart.main(Native Method)
01-18 21:43:46.038: E/AndroidRuntime(1608): Caused by: java.lang.reflect.InvocationTargetException
01-18 21:43:46.038: E/AndroidRuntime(1608): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 21:43:46.038: E/AndroidRuntime(1608): at java.lang.reflect.Method.invoke(Method.java:525)
01-18 21:43:46.038: E/AndroidRuntime(1608): at android.view.View$1.onClick(View.java:3673)
01-18 21:43:46.038: E/AndroidRuntime(1608): ... 11 more
01-18 21:43:46.038: E/AndroidRuntime(1608): Caused by: android.os.NetworkOnMainThreadException
01-18 21:43:46.038: E/AndroidRuntime(1608): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
01-18 21:43:46.038: E/AndroidRuntime(1608): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
01-18 21:43:46.038: E/AndroidRuntime(1608): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-18 21:43:46.038: E/AndroidRuntime(1608): at libcore.io.IoBridge.connect(IoBridge.java:112)
01-18 21:43:46.038: E/AndroidRuntime(1608): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-18 21:43:46.038: E/AndroidRuntime(1608): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
01-18 21:43:46.038: E/AndroidRuntime(1608): at java.net.Socket.startupSocket(Socket.java:566)
01-18 21:43:46.038: E/AndroidRuntime(1608): at java.net.Socket.tryAllAddresses(Socket.java:127)
01-18 21:43:46.038: E/AndroidRuntime(1608): at java.net.Socket.<init>(Socket.java:177)
01-18 21:43:46.038: E/AndroidRuntime(1608): at java.net.Socket.<init>(Socket.java:149)
01-18 21:43:46.038: E/AndroidRuntime(1608): at com.example.myfirstapp.EsmFamilActivity.sendToServer(EsmFamilActivity.java:61)
01-18 21:43:46.038: E/AndroidRuntime(1608): ... 14 more
01-18 21:43:53.518: I/Process(1608): Sending signal. PID: 1608 SIG: 9
01-18 21:43:53.668: D/TextView(2467): Constructor - Got Res id for appearance for textColorPrimaryInverse
01-18 21:43:53.668: W/ResourceType(2467): Skipping entry 0x1060085 in package table 0 because it is not complex!
01-18 21:43:53.668: D/TextView(2467): Constructor - Got appearance for textColorPrimaryInverse
01-18 21:43:53.668: D/TextView(2467): Constructor -- Got mEditTextBackgroundColor
01-18 21:43:53.998: D/libEGL(2467): loaded /vendor/lib/egl/libEGL_POWERVR_SGX544_115.so
01-18 21:43:54.078: D/libEGL(2467): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX544_115.so
01-18 21:43:54.088: D/libEGL(2467): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX544_115.so
01-18 21:43:54.088: E/IMGSRV(2467): :0: PVRDRMOpen: TP3, ret = 42
01-18 21:43:54.098: E/IMGSRV(2467): :0: PVRDRMOpen: TP3, ret = 45
01-18 21:43:54.098: E/IMGSRV(2467): :0: PVRDRMOpen: TP3, ret = 46
01-18 21:43:54.098: E/IMGSRV(2467): :0: PVRDRMOpen: TP3, ret = 46
01-18 21:43:54.098: E/IMGSRV(2467): :0: PVRDRMOpen: TP3, ret = 46
01-18 21:43:54.108: E/IMGSRV(2467): :0: PVRDRMOpen: TP3, ret = 48
01-18 21:43:54.138: D/OpenGLRenderer(2467): Enabling debug mode 0
答案 0 :(得分:0)
我知道android应用程序正在服务器桌面上的模拟器中运行。首先,您需要更改LAN IP 192.168.x.x
Blockquote使用Android模拟器时,您可能经常需要访问数据 使用localhost托管在本地Web服务器上。例如,您自己的Web服务 在开发过程中可能会在您的本地计算机上托管,您也可以 想要在用于编写Android的同一台开发机器上测试它们 应用。在这种情况下,您应该使用10.0.2.2的特殊IP地址(不是 127.0.0.1)指代主机的环回接口。来自Android 在模拟器的角度来看,localhost(127.0.0.1)指的是它自己的loopback接口。
然后,如果Android应用程序没有在服务器上运行,我猜你需要一些额外的行来避免错误:
serverAddress =
InetAddress.getByName(“192.168.1.142”);
//--remember to change the IP address above to match your own-- and also the port
socket = new Socket(serverAddress, 500);
而且我正在扩展你在扩展的AsyncTask类中做的。