Android.os.NetworkOnMainThreadException

Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p>After 4.0, Google forced the link network can not be accessed in the main thread, the main thread will be running this error</p></p><p><p></p></p><p><p>Resolve error You must create a child thread</p></p><pre><pre><span style="color: #0000ff">New</span> Thread () {<span style="color: #0000ff"></span>public<span style="color: #0000ff">void</span> method1 () {<br><span style="color: #008000">//</span> <span style="color: #008000">put the data you want to link to the network</span> }}. Start ();</pre></pre><pre><pre>With this sentence, the above error has been solved, but there are new Mistakes.</pre></pre><p><p></p></p><p><p>Because only the main thread (the UI Thread) can follow the new UI (if not, it can cause threads to synchronize with some complex cases of mutex ...). ∴ If you update the UI in a child thread, an error occurs</p></p><p><p>Workaround: Information mechanism (handler Principle)</p></p><p><p>1: define a handler in the main thread</p></p><pre><pre><span style="color: #008000">//</span> <span style="color: #008000">a Handler is defined in the main thread , and a class declaration followed by a curly brace {} means the method of overriding this class</span> <span style="color: #0000ff">private</span> Handler handler=<span style="color: #0000ff">new</span> <span style="color: #000000"> Handler () { </span><span style="color: #008000">//</span> <span style="color: #008000">This method is executed in the main</span> thread <span style="color: #000000"> @Override </span>public<span style="color: #0000ff"></span><span style="color: #0000ff">void</span> <span style="color: #000000">handlemessage (Message msg) { </span><span style="color: #008000">//</span> <span style="color: #008000">Update the UI you need</span> string content= (string) msg.obj ; <span style="color: #008000">//</span> <span style="color: #008000">Get information</span> Tv.settext (content); <span style="color: #008000">//</span> <span style="color: #008000">with the new UI</span> <span style="color: #000000"> }};</span></pre></pre><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">View Code</span></span><p><p>2: Child threads inside</p></p><pre><pre> <span style="color: #008000">//</span> <span style="color: #008000">calling the handler object tells the system that we want to talk to the new UI</span> <span style="color: #008000">//</span> <span style="color: #008000">1: Create a Message object</span> Message msg=<span style="color: #0000ff">new</span> <span style="color: #000000"> message (); Msg.obj</span>=content; <span style="color: #008000">//</span> <span style="color: #008000"> String content=streamtools.tooleadstream (inputstream);</span> <span style="color: #000000"> handler.sendmessage (msg); </span> <span style="color: #008000">//</span> <span style="color: #008000">sends a message information to send the data to the MSG , after sending the message, the main thread definition handler immediately executes the Handlemessage () method</span></pre></pre><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">View Code</span></span><p><p></p></p><p><p>Android.os.NetworkOnMainThreadException</p></p></span>

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.