Layout file:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Android:paddingbottom= "@dimen/activity_vertical_margin"7 Android:paddingleft= "@dimen/activity_horizontal_margin"8 Android:paddingright= "@dimen/activity_horizontal_margin"9 Android:paddingtop= "@dimen/activity_vertical_margin"Ten Tools:context= "Com.hanqi.testapp3.TestActivity2" One android:orientation= "vertical"> A - <Button - Android:layout_width= "Match_parent" the 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"/> A </LinearLayout>
Java class:
1 Packagecom.hanqi.testapp3;2 3 ImportAndroid.app.ProgressDialog;4 Importandroid.support.v7.app.AppCompatActivity;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.widget.EditText;8 ImportAndroid.widget.Toast;9 Ten ImportJava.io.InputStream; One Importjava.net.HttpURLConnection; A ImportJava.net.URL; - - Public classTestActivity2extendsappcompatactivity { the - EditText et_2; - @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_test2); +Et_2 =(EditText) Findviewbyid (r.id.et_2); A } at //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 in NewThread () { - Public voidRun () to { + Try { - //1-url theURL url =NewURL ("http://www.baidu.com" + "? Name=tom"); * $ //2-url getting the connectionPanax NotoginsengHttpURLConnection Huc =(HttpURLConnection) url.openconnection (); - //Request Method theHuc.setrequestmethod ("GET"); + //Set Timeout AHuc.setconnecttimeout (3000); theHuc.setreadtimeout (3000); + //Connect and send requests - Huc.connect (); $ //Receive $ //determine the return status code - intCode =Huc.getresponsecode (); - if(Code = = 200) the { - //Receive DataWuyi //input Stream theInputStream is =Huc.getinputstream (); - //Read Stream Wu //1-byte Array - byte[] B =New byte[1024]; About //read to the length of the array $ inti = 0; - //3-Data - FinalStringBuilder SB1 =NewStringBuilder (); - while((i = Is.read (b)) >0) A { +Sb1.append (NewString (b,0, i)); the } - //Freeing Resources $ is.close (); the Huc.disconnect (); the //Displaying Information and closing dialog boxes via the main thread theRunonuithread (NewRunnable () { the @Override - Public voidrun () { in Et_2.settext (SB1); the Progressdialog.dismiss (); the } About }); the } the Else the { +Toast.maketext (TestActivity2. This, "Connection error, return status code =" +code, Toast.length_short). Show (); - } the }Bayi Catch(Exception e) the { the e.printstacktrace (); - Progressdialog.dismiss (); - } the } the }.start (); the } the}
Get request mode for ANDROID--JDK