Android HTTP data communication obtains data through GET requests

Source: Internet
Author: User

Get data through GET request
Public static string sendgetrequest (string endpoint, <br/> string requestparameters) {<br/> log. I ("sendgetrequest", endpoint); <br/> string result = NULL; <br/> If (endpoint. startswith ("http ://")) {<br/> // send a GET request to the servlet <br/> try {<br/> // construct data <br/> stringbuffer DATA = new stringbuffer (); <br/> // send data <br/> string urlstr = endpoint; <br/> If (requestparameters! = NULL & requestparameters. Length ()> 0) {<br/> urlstr + = "? "+ Requestparameters; <br/>}< br/> log. I ("urlstr", urlstr); <br/> url = new URL (urlstr); <br/> urlconnection conn = URL. openconnection (); <br/> // get the response <br/> bufferedreader RD = new bufferedreader (New inputstreamreader (<br/> Conn. getinputstream (); <br/> stringbuffer sb = new stringbuffer (); <br/> string line; <br/> while (line = RD. readline ())! = NULL) {<br/> Sb. append (line); <br/>}< br/> Rd. close (); <br/> result = sb. tostring (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> log. I ("sendgetrequest", result); <br/> return result; <br/>}< br/> for example, by specifying the image of a URL, obtain the image data and convert it to the supported image format of Android. The Code is as follows: <br/> // display images on the Network <br/> Public static bitmap returnbitmap (string URL) {<br/> log. I ("returnbitmap", "url =" + URL); <br/> URL myfileurl = NULL; <br/> Bitmap bitmap = NULL; <br/> try {<br/> myfileurl = new URL (URL); <br/>} catch (malformedurlexception e) {<br/> E. printstacktrace (); <br/>}< br/> try {<br/> httpurlconnection conn = (httpurlconnection) myfileurl <br/>. openconnection (); <br/> Conn. setdoinput (true); <br/> Conn. connect (); <br/> inputstream is = Conn. getinputstream (); <br/> bitmap = bitmapfactory. decodestream (is); <br/> is. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> return bitmap; <br/>}

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.