Android. OS. NetworkOnMainThreadException the real machine cannot receive server data

Source: Internet
Author: User

Problem description: Server JSP, client android. After the server is released, the client can debug and receive the data sent from the server on the simulator, but the real machine cannot receive the data. An exception is reported: android. OS. NetworkOnMainThreadException.


Cause: the program executes network access on the main thread. in MySQL 4.0, the access network cannot be in the main program. This problem occurs when the network is connected to MySQL 3.0 or later. These are all the statements on the Internet. In short, thank you for your sharing ~~~


1. http://blog.csdn.net/dcl8261425/article/details/9181545

/AndroidRuntime (1185): android. OS. NetworkOnMainThreadException

This is because the program executes network access on the main thread. Can be reduced

Android: minSdkVersion = "8"
Android: targetSdkVersion = "8"/>

No exception is reported. But the best solution is to write the thread.


2. http://my.eoe.cn/iceskysl/archive/4382.html


public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    this.setContentView(R.layout.share_mblog_view);
// Call new Thread (runnable ). start ();} Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {super. handleMessage (msg); Bundle data = msg. getData (); String val = data. getString ("value"); Log. I ("mylog", "request result -->" + val) ;}} Runnable runnable = new Runnable () {@ Override public void run (){
// Write my request data code here
 
// Pass the value Message msg = new Message (); Bundle data = new Bundle (); data. putString ("value", "request result"); msg. setData (data); handler. sendMessage (msg );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.