Org. Apache. http. Conn. httphostconnectexception: connection to refused

Source: Internet
Author: User

Android has this problem:

Http://www.oschina.net/code/snippet_12_5909

Private void dopost (string Val) {// encapsulate the Data Map <string, string> parmas = new hashmap <string, string> (); parmas. put ("name", Val); defaulthttpclient client = new defaulthttpclient (); // HTTP client httppost = new httppost ("http://mhycoe.com/test/post.php "); arraylist <basicnamevaluepair> pairs = new arraylist <basicnamevaluepair> (); If (parmas! = NULL) {set <string> keys = parmas. keyset (); For (iterator <string> I = keys. iterator (); I. hasnext ();) {string key = (string) I. next (); pairs. add (New basicnamevaluepair (Key, parmas. get (key) ;}}try {urlencodedformentity p_entity = new urlencodedformentity (pairs, "UTF-8");/** put post data into HTTP Request */httppost. setentity (p_entity);/** send the actual http post request */httpresponse response = client.exe cute (httppost ); Httpentity entity = response. getentity (); inputstream content = entity. getcontent (); string returnconnection = convertstreamtostring (content); show. settext (returnconnection);} catch (illegalstateexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} private string convertstreamtostring (inputstream is) {bufferedreader reader = new bufferedreader (New inputstreamr Eader (is); stringbuilder sb = new stringbuilder (); string line = NULL; try {While (line = reader. Readline ())! = NULL) {sb. append (line) ;}} catch (ioexception e) {e. printstacktrace ();} finally {try {is. close ();} catch (ioexception e) {e. printstacktrace () ;}} return sb. tostring ();}

Solution: add permissions !!


<Uses-Permission Android: Name = "android. Permission. Internet"/>

If the following error occurs: Android. OS. networkonmainthreadexception:

Reference

Http://www.cnblogs.com/freexiaoyu/archive/2012/04/13/2445707.html

Note: The cause of this error is that the Code does not comply with the android standard. If you change the above access method to asynchronous operations, Android will not appear during access on 4.0. OS. networkonmainthreadexception exception: New thread () {@ overridepublic void run () {// method to be executed // send an empty message to handler after the execution is complete. sendemptymessage (0 );}}. start (); // define the handler object private handler = new handler () {@ override // when a message is sent, execute the handler public void handlemessage (Message MSG) {super. handlemessage (MSG); // process the UI }};

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.