One of the httpurlconnection used in Android

Source: Internet
Author: User

We have seen the use of the HttpURLConnection class before we learned about Java's network programming. Httpurlconnect in Java is a class that supports HTTP-specific functionality, which is often used in many network programming. Today I'm documenting the use of httpurlconnection in Android, and I've learned to use asynchronous loading to load a Web site using the same principle as multi-threading, but it uses JSON to parse a Web site, Instead of directly using the domain name is the URL to parse a site, and the httpurlconnection to be introduced is to use a domain name to parse a Web site, of course, it also uses the handler class, because this case uses the child thread to load the UI, the main thread to update the UI. The code is as follows

XML code

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical"6 Tools:context= "Com.example.android_http." Mainactivity " >7     <WebView8         Android:id= "@+id/webview"9 Android:layout_width= "Fill_parent"Ten Android:layout_height= "Fill_parent" One         /> A  - </LinearLayout>

As you can see from the XML above, we only use one control, which is webview. WebView has its own beauty in loading the website.

Java code

1. The first is the Mainactivity class

1  Packagecom.example.android_http;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.os.Handler;6 ImportAndroid.webkit.WebView;7 8  Public classMainactivityextendsActivity {9     PrivateWebView WebView =NULL;Ten     PrivateHandler Handler =NewHandler (); One @Override A     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); theWebView =(WebView) Findviewbyid (R.id.webview); -         NewHttpthread ("http://www.baidu.com", WebView, Handler). Start (); -     } -  +}

2. followed by the Httpthread class, which inherits the class thread class, creates a child thread to load the Web page

1  Packagecom.example.android_http;2 3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStreamReader;6 Importjava.net.HttpURLConnection;7 Importjava.net.MalformedURLException;8 Importjava.net.ProtocolException;9 ImportJava.net.URL;Ten  One Importjavax.net.ssl.HttpsURLConnection; A  - ImportAndroid.os.Handler; - ImportAndroid.webkit.WebView; the  -  Public classHttpthreadextendsthread{ -     PrivateString URL =NULL; -     PrivateWebView WebView =NULL; +     PrivateHandler Handler =NULL; -      Publichttpthread (String URL, WebView WebView, Handler Handler) +     { A          This. Handler =handler; at          This. url =URL; -          This. WebView =WebView; -     } - @Override -      Public voidrun () { -         /* in * About the use of httpurlconnection, what do not understand, please go back to carefully review the Java Network programming -          */ toHttpURLConnection httpurlconnection =NULL; +         Try { -URL url =NewURL ( This. URL);  theHttpURLConnection =(HttpURLConnection) url.openconnection (); *}Catch(malformedurlexception e) { $             //TODO auto-generated Catch blockPanax Notoginseng e.printstacktrace (); -}Catch(IOException e) { the             //TODO auto-generated Catch block + e.printstacktrace (); A         } the         //allow timeout of 5 seconds +         Try { -Httpurlconnection.setreadtimeout (5000); $             //connect to the server in a get way $Httpurlconnection.setrequestmethod ("GET"); -}Catch(protocolexception e) { -             //TODO auto-generated Catch block the e.printstacktrace (); -         }Wuyi         FinalStringBuffer SB =NewStringBuffer (); theString str =NULL; -         Try { WuBufferedReader br =NewBufferedReader (NewInputStreamReader (Httpurlconnection.getinputstream ())); -             Try { About                  while(str = br.readline ())! =NULL) $                 { - sb.append (str); -                 } -}Catch(IOException e) { A                 //TODO auto-generated Catch block + e.printstacktrace (); the             } -}Catch(IOException e) { $             //TODO auto-generated Catch block the e.printstacktrace (); the         } theHandler.post (NewRunnable () { the  -              Public voidrun () { in                 //The first parameter represents the data that needs to be loaded-this has previously been read into SB in the input stream the                 //The second parameter represents the encoding format in which the Web site needs to be loaded-Baidu's encoding format is so the                 /* About * Attention is paid to the following: the * LoadData do not load pictures, if you want to load pictures, please use Loaddatawithbaseurl the                  */ theWebview.loaddata (Sb.tostring (), "Text/html;charset=utf-8",NULL); +                  -             } the         });Bayi     } the  the}

One of the httpurlconnection used in Android

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.