简体   繁体   中英

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.

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.

Here a Link to the Exception that (i think) is thrown by your app (look at your logcat for more information). NetworkOnMainThreadException

Try to do all your network activities with an AsyncTask .

I hope this will help you. =)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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