I. HTTP protocol
1. Hypertext Transfer Protocol
2. Support client/server-side mode
3. Content
1-Request Agreement
1> Request Message Format
1>-Request Line: Request Resource Name protocol version number;
2>-Request message Header
3>-Request Body
2> Request method
1>-post: Request content in the request body, in the form of key = value, key value pair with & interval, the length is unrestricted, the confidentiality is high.
2>-get: The request content is used after the URL? Starting with the key = value in the form of & interval between key-value pairs, the request message has no request, the length of the request data is limited by the browser, and the request data confidentiality is low.
2-Response Protocol: Response message Format
1> Response Status line: Describes the server processing result; The response status code is 200 successful.
2> Response message Header
3> Response Body
Two. Mode
1.JDK mode
2.Android mode
3. Frame mode
Code Show: Get Request method for JDK
First set up network access rights:
<uses-permission android:name= "Android.permission.INTERNET"/>
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.TestActivity3" 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 at </LinearLayout>
. XML
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 classTestActivity3extendsappcompatactivity { the - EditText et_2; - - @Override + protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (R.LAYOUT.ACTIVITY_TEST3); A atEt_2=(EditText) Findviewbyid (r.id.et_2); - } - - //get method for JDK - Public voidBt1_onclick (View v) - { in //1. Progress dialog Box - FinalProgressDialog Progressdialog=progressdialog.show ( This,NULL, "Loading, please wait ..."); to + //2. Turn on the sub-thread to access the network - NewThread () { the @Override * Public voidrun () { $ Panax Notoginseng Try { - the //1-url +URL url =NewURL (Et_2.gettext (). toString () + "? Name=tom"); A the //2-url getting the connection +HttpURLConnection huc=(HttpURLConnection) url.openconnection (); - $ //Request Method $Huc.setrequestmethod ("GET"); - - //Set Timeout theHuc.setconnecttimeout (3000); -Huc.setreadtimeout (3000);Wuyi the //Connect and send requests - Huc.connect (); Wu - //Receive: About //determine the return status code $ intCode=Huc.getresponsecode (); - - if(code==200) - { A //Receive Data + the //input stream: -InputStream is=Huc.getinputstream (); $ the //Read Stream the the //1-byte Array the byte[] b=New byte[1024]; - in //read to the length of the array the intI=0; the About //3-Data the FinalStringBuilder sbl=NewStringBuilder (); the the while((I=is.read (b)) >0) + { -Sbl.append (NewString (b,0, i)); the }Bayi the //Freeing Resources the is.close (); - - Huc.disconnect (); the the //Displaying Information and closing dialog boxes via the main thread theRunonuithread (NewRunnable () { the @Override - Public voidrun () { the the Et_2.settext (SBL); the 94 Progressdialog.dismiss (); the } the }); the }98 Else About { -Toast.maketext (TestActivity3. This, "Connection error, return status code =" +code, Toast.length_short). Show ();101 }102 103 104}Catch(Exception e) { the 106 e.printstacktrace ();107 108 Progressdialog.dismiss ();109 the }111 } the }.start ();113 the the } the}
. Java
Get request mode for JDK stored by remote server