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 }};