Data store--GET request mode for remote server storage--JDK

Source: Internet
Author: User

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

Get request mode code for JDK

Set Network access Permissions first:

<uses-permission android:name= "Android.permission.INTERNET"/>

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:paddingbottom= "@dimen/activity_vertical_margin"7android:paddingleft= "@dimen/activity_horizontal_margin"8android:paddingright= "@dimen/activity_horizontal_margin"9android:paddingtop= "@dimen/activity_vertical_margin"Tentools:context= "Com.hanqi.testapp3.TestActivity3" Oneandroid:orientation= "Vertical" > A  -<Button -Android:layout_width= "Match_parent" theandroid:layout_height= "Wrap_content" -android:text= "Jdk-get Way" -android:onclick= "Bt1_onclick"/> -  +<EditText -Android:layout_width= "Match_parent" +android:layout_height= "200DP" AAndroid:id= "@+id/et_2"/> 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); A Setcontentview (R.LAYOUT.ACTIVITY_TEST3); at  -Et_2=(EditText) Findviewbyid (r.id.et_2); -     } -  -     //get method for JDK -      Public voidBt1_onclick (View v) in     { -         //1. Progress dialog Box to         FinalProgressDialog ProgressDialog = Progressdialog.show ( This,NULL, "Loading, please later ..."); +  -         //2. Turn on the sub-thread to access the network the         NewThread () { *              Public voidRun () $             {Panax Notoginseng                 Try { -                 //1.URL theURL url =NewURL (Et_2.gettext (). toString () + "? Name=tom"); +  A                 //2.URL getting the connection theHttpURLConnection Huc =(HttpURLConnection) url.openconnection (); +  -                 //Request Method $Huc.setrequestmethod ("GET"); $  -                 //Set Timeout -Huc.setconnecttimeout (3000); theHuc.setreadtimeout (3000); - Wuyi                 //Connect and send requests the Huc.connect (); -  Wu                 //Receive -                 //determine the return status code About                 intCode =Huc.getresponsecode (); $  -                 if(Code = = 200) { -                     //Receive Data -                     //input Stream AInputStream is =Huc.getinputstream (); +  the                     //Read Stream -  $                     //1.byte Arrays the                     byte[] B =New byte[1024]; the  the                     //2. Read the length of the array the                     inti = 0; -  in                     //3. Data the                     FinalStringBuilder SBL =NewStringBuilder (); the  About                      while((i = Is.read (b)) > 0) { theSbl.append (NewString (b, 0, i)); the                     } the  +                     //Freeing Resources - is.close (); the Huc.disconnect ();Bayi  the                     //Displaying Information and closing dialog boxes via the main thread theRunonuithread (NewRunnable () { - @Override -                          Public voidrun () { the Et_2.settext (SBL); the  the Progressdialog.dismiss (); the                         } -                     }); the}Else { theToast.maketext (TestActivity3. This, "Connection error, return status code =" +code, Toast.length_short). Show (); the                 }94             } the                 Catch(Exception e) the                 { the e.printstacktrace ();98  About Progressdialog.dismiss (); -                 }101             }102 }.start ();103     }104}
. Java

Data store--GET request mode for remote server storage--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.