Get request mode for JDK

Source: Internet
Author: User

Layout

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.hanqi.testapp3.TestActivity2"android:orientation= "Vertical" > <Button android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Jdk-get Way"Android:onclick= "Bt1_onclick"/> <EditText android:layout_width= "Match_parent"Android:layout_height= "200DP"Android:id= "@+id/et_2"/></linearlayout>
View Code

Activity:

 Packagecom.hanqi.testapp3;ImportAndroid.app.ProgressDialog;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL; Public classTestActivity2extendsappcompatactivity {EditText et_2; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.LAYOUT.ACTIVITY_TEST2); Et_2=(EditText) Findviewbyid (r.id.et_2); }    //get method for JDK     Public voidBt1_onclick (View v) {//1. Progress dialog Box        FinalProgressDialog ProgressDialog = Progressdialog.show ( This,NULL, "Loading, please later ..."); //2. Turn on the sub-thread to access the network        NewThread () { Public voidrun () {Try {                    //1-urlURL url =NewURL ("http://www.baidu.com" + "? Name=tom"); //2-url getting the connectionHttpURLConnection Huc =(HttpURLConnection) url.openconnection (); //Request MethodHuc.setrequestmethod ("GET"); //Set TimeoutHuc.setconnecttimeout (3000); Huc.setreadtimeout (3000); //Connect and send requestsHuc.connect (); //Receive//determine the return status code                    intCode =Huc.getresponsecode (); if(Code = = 200)                    {                        //Receive Data//input StreamInputStream is =Huc.getinputstream (); //Read Stream//1-byte Array                        byte[] B =New byte[1024]; //read to the length of the array                        inti = 0; //3-Data                        FinalStringBuilder SB1 =NewStringBuilder ();  while((i = Is.read (b)) >0) {sb1.append (NewString (b,0, i)); }                        //Freeing ResourcesIs.close ();                        Huc.disconnect (); //Displaying Information and closing dialog boxes via the main threadRunonuithread (NewRunnable () {@Override Public voidrun () {et_2.settext (SB1);                            Progressdialog.dismiss ();                    }                        }); }                    Else{toast.maketext (TestActivity2). This, "Connection error, return status code =" +code, Toast.length_short). Show (); }                }                Catch(Exception e) {e.printstacktrace ();                Progressdialog.dismiss ();    }}}.start (); }}
View Code

Get request mode for JDK

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.