HttpHostConnectException and NetworkOnMainThreadException

Source: Internet
Author: User

I used HttpClient AS THE post request today and reported an error.

First error: HttpHostConnectException: Connection to http://192.168.xx.xx refused

You just need to change the IP address to http: // 10.0.2.2 ~ This is the network segment of the simulator.

Or 127.0.0.1 or localhost, which indicates the virtual machine itself.

---- Add the following content to the configuration file manifest. xml:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" ></uses-permission>    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" ></uses-permission>    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" ></uses-permission>    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission>    <uses-permission android:name="android.permission.INTERNET" ></uses-permission>    <uses-permission android:name="android.permission.WAKE_LOCK" ></uses-permission>

Permission. WIFI or something can be deleted


Another error:

Project is 2.2 development and testing mobile phone is android4.0 prompt android. OS. networkOnMainThreadException exception. Originally in 4.0, the access network cannot be performed in the main program. There are two solutions, one being added to the main program:

New Thread () {@ Override public void run () {// method to be executed // send an empty message to handler after execution. sendEmptyMessage (0 );}}. start (); // define the Handler object private Handler handler = new Handler () {@ Override // when a Message is sent, execute the Handler public void handleMessage (Message msg) {super. handleMessage (msg); // process the UI }};


If the request is written in the main process, it does not comply with android specifications. It will also cause the user interface to die.



This article is from the "FlyHome" blog, please be sure to keep this source http://superfly81.blog.51cto.com/6302302/1279036

Related Article

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.