HttpURLConnection GET Request

Source: Internet
Author: User

1  Packagecom.example.administrator.myapplication.activity;2 3 ImportAndroid.os.Bundle;4 ImportAndroid.os.Handler;5 ImportAndroid.os.Message;6 Importandroid.support.v7.app.AppCompatActivity;7 ImportAndroid.view.View;8 ImportAndroid.webkit.WebView;9 ImportAndroid.widget.Button;Ten  One ImportCOM.EXAMPLE.ADMINISTRATOR.MYAPPLICATION.R; A  - Importjava.io.IOException; - ImportJava.io.InputStream; the Importjava.net.HttpURLConnection; - Importjava.net.MalformedURLException; - ImportJava.net.URL; -  +  Public classHttpurlconnectiongetactivityextendsappcompatactivity { - WebView WebView; + Button httpurlbtn; A @Override at     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_get); -WebView =(WebView) Findviewbyid (R.id.baidu); -HTTPURLBTN =(Button) Findviewbyid (r.id.httpurl); -Httpurlbtn.setonclicklistener (NewView.onclicklistener () { in @Override -              Public voidOnClick (View v) { to                 /*thread thread = new Thread (new Runnable () { + @Override - Public void Run () { the httpurlconnectionget (); *                     } $                 });Panax Notoginseng Thread.Start ();*/ -                 //!! Network request Httpurlconnectionget () to be placed in a child thread the                 NewThread (NewRunnable () { + @Override A                      Public voidrun () { the httpurlconnectionget (); +                     } - }). Start (); $             } $         }); -     } -     //GET Request the     Private voidHttpurlconnectionget () { -HttpURLConnection httpurlconnection =NULL;WuyiInputStream is =NULL; the         //StringBuilder: Thread is not secure, can have multi-threading, faster than Stingbuffer, usage with StringBuffer -         //stringbuffer: Thread safe, only single threaded WuStringBuilder SB =NewStringBuilder (); -         Try { About             //Prepare the requested network address $URL url =NewURL ("Http://apis.baidu.com/txapi/weixin/wxhot?num=10&page=1&word=%E7%9B%97%E5%A2%93%E7%AC%94%E8%AE%B0"); -             //call OpenConnection to get a network connection and the network connection is in a ready state -HttpURLConnection =(HttpURLConnection) url.openconnection (); -             //Set network connection timeout time 5S AHttpurlconnection.setconnecttimeout (5*1000); +             //set the Read timeout time theHttpurlconnection.setreadtimeout (5*1000); -Httpurlconnection.setrequestproperty ("Apikey", "58218dcc8845195b277082c3a357f481"); $ Httpurlconnection.connect (); the             //If connection request code succeeded the             if(Httpurlconnection.getresponsecode () = =HTTPURLCONNECTION.HTTP_OK) { theis =Httpurlconnection.getinputstream (); the                 byte[] bytes =New byte[1024]; -                 inti = 0; in                  while((i = Is.read (bytes))! =-1){ theSb.append (NewString (Bytes,0,i, "Utf-8")); the                 } About is.close (); the             } the}Catch(malformedurlexception e) { the e.printstacktrace (); +}Catch(IOException e) { - e.printstacktrace (); the}finally {Bayi             if(HttpURLConnection! =NULL){ the Httpurlconnection.disconnect (); the             } -         } -         //Send message: What message type, obj message content theMessage message = Handler.obtainmessage (1, sb.tostring ()); the handler.sendmessage (message); the     } the  -     //Handler: Message processing mechanism (sending messages, processing messages), can only be placed in the main thread theHandler Handler =NewHandler () { the @Override the          Public voidhandlemessage (Message msg) {94             Super. Handlemessage (msg); the             if(msg! =NULL&& Msg.what ==1){ theString s =(String) msg.obj; theWebview.getsettings (). Setdefaulttextencodingname ("Utf-8");98Webview.getsettings (). setjavascriptenabled (true); AboutWebview.loaddatawithbaseurl (NULL, S, "text/html", "Utf-8",NULL); -             }101         }102     };103}
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Xmlns:tools= "Http://schemas.android.com/tools"4Android:layout_width= "Match_parent"5android:layout_height= "Match_parent"6android:orientation= "Vertical"7Tools:context= ". Activity. Httpurlconnectiongetactivity ">8 9<WebViewTenAndroid:id= "@+id/baidu" OneAndroid:layout_width= "Match_parent" Aandroid:layout_height= "400DP"/> -  -<Button theAndroid:id= "@+id/httpurl" -Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" -android:text= "HttpURLConnection GET Request" +Android:textallcaps= "false"/> -  +</LinearLayout>

HttpURLConnection GET Request

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.