简体   繁体   English

基于套接字连接的应用程序在Android 2.3中效果很好,但在Android 4.2中效果不佳

[英]Application based on socket connection, works well in Android 2.3 but not in android 4.2

I have developed an android app, which works well in android 2.3 but not in android 4.2. 我已经开发了一个android应用程序,它在android 2.3中效果很好,但在android 4.2中效果不佳。

The app creates a socket connection and sends data to the server. 该应用程序创建套接字连接,并将数据发送到服务器。 The problem is with socket connection. 问题出在套接字连接上。 Please help me. 请帮我。

The code for socket connection is: 套接字连接的代码是:

Socket socket = new Socket();
int timeout=30000;
socket.connect(sockaddr, timeout);
//sockaddr is user defined.
//sockaddr address has server ip address and port

since Android Version 3.0 (API 11) it is not allowed to make network activity on the main thread of your app. 从Android版本3.0(API 11)开始,不允许在应用程序的主线程上进行网络活动。

Here a Link to the Exception that (i think) is thrown by your app (look at your logcat for more information). 这里是您的应用抛出的(我认为)异常的链接(有关更多信息,请参见logcat)。 NetworkOnMainThreadException NetworkOnMainThreadException

Try to do all your network activities with an AsyncTask . 尝试使用AsyncTask进行所有网络活动。

I hope this will help you. 我希望这能帮到您。 =) =)

从Android 3.0开始,您需要异步执行Socket连接,主线程不允许这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM