Android sends HTTP requests

Source: Internet
Author: User

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

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.