无法创建Android客户端套接字 [英] unable to create android client socket

查看:223
本文介绍了无法创建Android客户端套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个连接到我的桌面上一台服务器一个Android客户端应用程序。结果
我成功地telneted从我的笔记本电脑服务器。结果
我已经把互联网和网络权限清单文件。结果
创建套接字和Eclipse debuuger得到我socket.class源文件时程序崩溃。

更新:
这里是我的完整code

 公共类EsmFamilActivity延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_esm_famil);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.esm_famil,菜单);
    返回true;
}//处理程序sendtoserver
公共无效sendToServer(查看视图){    ...
    字符串主机=192.168.1.7;
    INT端口= 8888;
    尝试{
        插座S =新的Socket(主机,端口);
        作家出=新OutputStreamWriter(s.getOutputStream());
        out.write(数据+\\ n);
        了out.flush();
        S.CLOSE();
    }赶上(前的UnknownHostException){
        ex.printStackTrace();
    }赶上(IOException异常前){
        ex.printStackTrace();
    }}

}

和这里是我的logcat

  01-18 21:43:06.908:D / TextView的(1608):构造 -  GOT mEditTextBackgroundColor
01-18 21:43:09.658:W / IInputConnectionWrapper(1608):beginBatchEdit上的非活动InputConnection
01-18 21:43:09.668:W / IInputConnectionWrapper(1608):endBatchEdit上的非活动InputConnection
01-18 21:43:11.448:W / IInputConnectionWrapper(1608):beginBatchEdit上的非活动InputConnection
01-18 21:43:11.448:W / IInputConnectionWrapper(1608):endBatchEdit上的非活动InputConnection
01-18 21:43:46.0​​18:D / AndroidRuntime(1608):关闭VM
01-18 21:43:46.0​​18:W / dalvikvm(1608):主题ID = 1:螺纹未捕获的异常退出(组= 0x42f02500)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):致命异常:主要
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):java.lang.IllegalStateException:无法执行活动的方法
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在android.view.View $ 1.onClick(View.java:3678)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在android.view.View.performClick(View.java:4300)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在android.view.View $ PerformClick.run(View.java:18026)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在android.os.Handler.handleCallback(Handler.java:730)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在android.os.Handler.dispatchMessage(Handler.java:92)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在android.os.Looper.loop(Looper.java:150)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在android.app.ActivityThread.main(ActivityThread.java:5162)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在java.lang.reflect.Method.invokeNative(本机方法)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在java.lang.reflect.Method.invoke(Method.java:525)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在dalvik.system.NativeStart.main(本机方法)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):因:java.lang.reflect.InvocationTargetException
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在java.lang.reflect.Method.invokeNative(本机方法)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在java.lang.reflect.Method.invoke(Method.java:525)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在android.view.View $ 1.onClick(View.java:3673)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):11 ...更多
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):因:android.os.NetworkOnMainThreadException
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在libcore.io.IoBridge.connect(IoBridge.java:112)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在java.net.Socket.startupSocket(Socket.java:566)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在java.net.Socket.tryAllAddresses(Socket.java:127)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在java.net.Socket中的<&初始化GT;(Socket.java:177)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在java.net.Socket中的<&初始化GT;(Socket.java:149)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):在com.example.myfirstapp.EsmFamilActivity.sendToServer(EsmFamilActivity.java:61)
01-18 21:43:46.0​​38:E / AndroidRuntime(1608):14 ...更多
01-18 21:43:53.518:I /流程(1608):发送信号。 PID:1608 SIG:9
01-18 21:43:53.668:D / TextView的(2467)构造函数 - GOT RES ID为外观textColorPrimaryInverse
01-18 21:43:53.668:W /的ResourceType(2467):在包表0跳过条目0x1060085,因为它不是复杂!
01-18 21:43:53.668:D / TextView的(2467)构造函数 - GOT外观textColorPrimaryInverse
01-18 21:43:53.668:D / TextView的(2467)构造函数 - GOT mEditTextBackgroundColor
01-18 21:43:53.998:D / libEGL(2467):加载/vendor/lib/egl/libEGL_POWERVR_SGX544_115.so
01-18 21:43:54.078:D / libEGL(2467):加载/vendor/lib/egl/libGLESv1_CM_POWERVR_SGX544_115.so
01-18 21:43:54.088:D / libEGL(2467):加载/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):启用调试模式0


解决方案

我理解的是,Android应用在服务器桌面上的模拟器中运行。所以,首先你需要更改局域网IP 192.168.x.x


  

大段引用当使用Android模拟器时,您可能经常需要访问的数据
  主办使用本地主机的本地Web服务器上。例如,您自己的Web服务
  很可能在开发过程中在本地计算机上进行托管,你会
  要测试他们,你用它来写你的Andr​​oid相同的开发机器上
  应用程序。在这种情况下,你应该使用的10.0.2.2特殊的IP地址(而不是
  127.0.0.1)来表示主计算机的环回接口。从Android
  模拟器的角度来看,本地主机(127.0.0.1)指的是自己的loopback接口。


http://p2p.wrox.com/book引

-beginning-Android的应用程序开发-631 /

这时如果Android应用程序是不是在服务器上运行我猜你需要一些额外的线条来避免错误:

  serverAddress =
InetAddress.getByName(192.168.1.142);
// - 记得更改上面的IP地址来匹配您的own--也是端口
插座=新的Socket(serverAddress,500);

和还我asumming你正在做内部的延伸AsyncTask的类。

I'm trying to create an android client app that connects to a server on my desktop.
I successfully telneted to the server from my laptop.
I have put INTERNET and NETWORK permissions in manifest file.
The program crashes when creating the socket and eclipse debuuger gets me to socket.class source file.

UPDATE: here's my complete code

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();
    }

}

}

and here is my 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

解决方案

I understood that the android app is running in the emulator on the server desktop. So first of all you need to change that LAN IP 192.168.x.x

Blockquote When working with the Android emulator, you may frequently need to access data hosted on the local web server using localhost. For example, your own web services are likely to be hosted on your local computer during development, and you’ll want to test them on the same development machine you use to write your Android applications. In such cases, you should use the special IP address of 10.0.2.2 (not 127.0.0.1) to refer to the host computer’s loopback interface. From the Android emulator’s perspective, localhost (127.0.0.1) refers to its own loopback interface.

cited from http://p2p.wrox.com/book-beginning-android-application-development-631/

Then if the android app is not in running in the server I guess you need some extra lines to avoid errors:

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);

And also I'm asumming you're doing inside the extends AsyncTask class.

这篇关于无法创建Android客户端套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆