Long time no blog, because the company to do Android, the author is also the first contact.
This is a more troublesome problem to be encountered in the project. Record the memo (I just got in touch.) Please advise if there is an incorrect place).
Code to send the request:
Package Com.jiujian.mperdiem;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import Java.io.inputstreamreader;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.url;public class Apputil {//local test path public static final String Webbaseurl = "HTTP://IP: Port"; /* * Access URL. Gets the result method:get, POST */public static string Loadurlresponse (String method, String urlstring) {HttpURLConnection conn = null; Connection object InputStream is = null; StringBuffer result = new StringBuffer (); try {URL url = new URL (urlstring);//URL Object conn = (httpurlconnection) url.openconnection ();//Use URL to open a link Conn.setdoinput (TRUE); Agree to the input stream, that is, agree to download conn.setdooutput (true); Agree to the output stream, that is, agree to upload conn.setusecaches (false); Do not use buffer Conn.setrequestmethod (method); Use GET request is = Conn.getinputstream (); Gets the input stream. At this point the link inputstreamreader ISR = new InputStreamReader is really established; BufferedReader Bufferreader = nEW BufferedReader (ISR); String inputline = ""; while ((Inputline = Bufferreader.readline ()) = null) {result.append (Inputline). Append ("\ n"); }} catch (Malformedurlexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } finally {if (is = null) {try {is.close (); } catch (IOException e) {e.printstacktrace (); }} if (conn! = null) {conn.disconnect (); }} return result.tostring (); }}
Calling code:
StringBuffer Sbupdatedevicerefreshinstall = new StringBuffer (Apputil.webbaseurl); Sbupdatedevicerefreshinstall.append ("XXX?Userid= "); Sbupdatedevicerefreshinstall.append (GetUserId ()); Apputil.loadurlresponse ("POST", sbupdatedevicerefreshinstall.tostring ());
There is no problem with the code, but the app sends the request. The server side has not been printing information.
the error message is: android.os.NetworkOnMainThreadException
Finally found that Android 3.0 is different from the main thread on the Internet access to ask,
Then change the code to:
New Thread () { @Override public void Run () { StringBuffer sbupdatedevicerefreshinstall = new StringBuffer ( Apputil.webbaseurl); Sbupdatedevicerefreshinstall.append ("XXX? Userid= "<span style=" font-family: ' Microsoft Yahei '; " >);</span> sbupdatedevicerefreshinstall.append (GetUserId ()); Apputil.loadurlresponse ("POST", sbupdatedevicerefreshinstall.tostring ()); } }. Start ();
That's fine.
Let's say it's just a touch of Android, it's recommended: The first line of code, this book is good for getting started.
Personal homepage:http://www.itit123.cn/ Many other dry goods waiting for you to get
Android sends HTTP requests