HttpURLConnection inherits the URLConnection, so it is also able to send a GET request, POST request to the specified site. It provides an easy way to urlconnetion based on the following examples.
int Getresponsecode (): Gets the response code of the server.
String getresponsemessage (): Gets the access information for the server.
String Getrequestmethod (): Gets the method that sent the request.
void Setrequestmethod (String method): Sets the method by which the request is sent.
Let's take a look at two examples:
first, the content of the site is displayed on the interface:
1. Create a WebView
<WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" />
2, Mainactivity.java
Public class mainactivity extends Activity { PrivateWebView WebView;//Update the UI of the main interface via handler on the child thread PrivateHandler Handler =NewHandler ();@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); WebView = (webview) Findviewbyid (R.id.webview);NewHttpthread ("Http://www.sina.com", WebView, Handler). Start (); }}
3. Create a child thread
Public class httpthread extends Thread { PrivateString URL;PrivateWebView WebView;PrivateHandler Handler;//Pass three parameters through a constructor function Public Httpthread(String URL, WebView WebView, Handler Handler) { This. url = URL; This. WebView = WebView; This. handler = handler; } }@Override Public void Run() {Try{URL Httpuri =NewURL (URL); HttpURLConnection conn = (httpurlconnection) httpuri.openconnection (); Conn.setreadtimeout ( the); Conn.setrequestmethod ("GET");FinalStringBuffer SB =NewStringBuffer (); BufferedReader reader =NewBufferedReader (NewInputStreamReader (Conn.getinputstream ())); String str; while(str = reader.readline ())! =NULL) {sb.append (str); } handler.post (NewRunnable () {@Override Public void Run() {//Load data into WebViewWebview.loaddata (Sb.tostring (),"Text/html;charset=utf-8",NULL); } }); }Catch(Malformedurlexception e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } }}
4. Add Permissions in Androidmanifest:
Android:name= "Android.permission.INTERNET"
download a picture according to the URL address
1. Create a WebView
<ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent"/>
2, Mainactivity.java
Public class mainactivity extends Activity { PrivateHandler Handler =NewHandler ();PrivateImageView ImageView;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); ImageView = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);NewHttpthread ("Http://img5.duitang.com/uploads/item/201410/23/20141023211631_VKmfz.jpeg", ImageView, Handler). Start (); }
3. Create a child thread
Public class httpthread extends Thread { PrivateString URL;PrivateHandler Handler;PrivateImageView ImageView; Public Httpthread(String URL, ImageView ImageView, Handler Handler) { This. url = URL; This. ImageView = ImageView; This. handler = handler; }@Override Public void Run() {Try{URL Httpuri =NewURL (URL); HttpURLConnection conn = (httpurlconnection) httpuri.openconnection (); Conn.setreadtimeout ( the); Conn.setrequestmethod ("GET");//set to read to input streamConn.setdoinput (true); InputStream in = Conn.getinputstream (); FileOutputStream out =NULL; String fileName = string.valueof (System.currenttimemillis ()); File DownloadFile =NULL;if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//Get the SDcard folder fileFile parent = Environment.getexternalstoragedirectory (); DownloadFile =NewFile (parent, fileName); out =NewFileOutputStream (DownloadFile); }//Buffer byte[] B =New byte[2*1024x768];intLenif(Out! =NULL) { while(len = In.read (b))! =-1) {Out.write (b,0, Len); } }FinalBitmap Bitmap = Bitmapfactory.decodefile (Downloadfile.getabsolutepath ()); Handler.post (NewRunnable () {@Override Public void Run() {imageview.setimagebitmap (bitmap); } }); }Catch(Malformedurlexception e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } }
4. Add Permissions in Androidmanifest:
Android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"
The downloaded image is displayed on the interface.
[using HttpURLConnection in Android base]android