Android_http Get and Post

Source: Internet
Author: User

Record

1. HTTP request Type:
Get: Get resources by requesting URLs
Post: Used to submit new content to the server
Put: Used to modify a content
Delete: Used to delete a content
Options: To view the performance of the server
Trace: For remote diagnostics server
.......
In the long time, other methods gradually withdrew from the historical stage, the most commonly used only get and post methods.

2. The difference between get and post methods
The Get method is like a postcard, only the request header. The Post method is like a letter with a request header and a request body.
The -1-get method (Idempotent method) is used to retrieve data from the server, and the Post method is used to submit data to the server.
The amount of data submitted to the server using the Get method is small, usually less than 2K, and the amount of data submitted to the server using post is usually unlimited.
The -3-get request is to attach the data that is being submitted to the URL, and the POST request is to place the submitted data in the requested request body.
GET Request Sample: http://192.168.1.100:8081/s02e14.jsp?name=away&pwd=123456

3. How to send data to the server using GET and post
Xml:

    <edittext        android:id= "@+id/nametext"        android:layout_width= "match_parent"        android:layout_height= "Wrap_content"        android:hint= "username"/>        <edittext        android:id= "@+id/pwdtext"        android: Layout_width= "Match_parent"        android:layout_height= "wrap_content"        android:layout_below= "@id/nametext"        android:hint= "password"/>        <button         android:id= "@+id/btn"        android:layout_width= "match _parent "        android:layout_height=" wrap_content "        android:layout_below=" @id/pwdtext "        android:text=" Login "/>
Activity
Package Com.away.b_10_httpgetandpost;import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.util.arraylist;import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.namevaluepair;import Org.apache.http.client.httpclient;import Org.apache.http.client.entity.urlencodedformentity;import Org.apache.http.client.methods.httpget;import Org.apache.http.client.methods.httppost;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.message.basicnamevaluepair;import Android.app.activity;import Android.os.Bundle;import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; Import Android.widget.edittext;public class Mainactivity extends Activity {private EditText nametext;private EditText Pwdtext;private Button btn; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); nametext = (EdittexT) Findviewbyid (r.id.nametext);p Wdtext = (EditText) Findviewbyid (r.id.pwdtext); btn = (Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {String name = Nametext.gettext (). ToString (); String pwd = Pwdtext.gettext (). toString ();//Use the Post method to send a request to the server Postthread pt = new Postthread (name, pwd);p t.start ();// Use the Get method to send a request to the server//getthread GT = new GetThread (name, pwd);//gt.start ();});} The thread uses the Post method to send a request to the server class Postthread extends thread {String name; String Pwd;public postthread (string name, string pwd) {this.name = Name;this.pwd = pwd;} @Overridepublic void Run () {HttpClient HttpClient = new Defaulthttpclient (); String url = "http://192.168.1.103:8080/get01.jsp";//Generate Request object using POST method HttpPost HttpPost = new HttpPost (URL);// The Namevaluepair object represents a key value that needs to be sent to the server Namevaluepair Pair1 = new Basicnamevaluepair ("name", name); Namevaluepair Pair2 = new Basicnamevaluepair ("pwd", pwd);//Place the prepared key-value object in a list arraylist<namevaluepair> pairs = new Arraylist< Namevaluepair> ();p airs.add (PAIR1);p airs.add (PAIR2);//Create an object that represents the request body try {httpentity requestentity = new Urlencodedformentity (pairs);//The request body is placed in the Request Object Httppost.setentity (requestentity);//Execute Request object try {HttpResponse response = Httpclient.execute (HttpPost); if (Response.getstatusline (). Getstatuscode () = = () {httpentity entity = Response.getentity (); BufferedReader reader = new BufferedReader (New InputStreamReader (Entity.getcontent ())); String result = Reader.readline (); LOG.D ("HTTP", "Post:" + result);}} catch (Exception e) {e.printstacktrace ();}} catch (Exception e) {e.printstacktrace ();}}} The thread uses the Get method to send the request to the server class GetThread extends thread {String name; String Pwd;public getthread (string name, string pwd) {this.name = Name;this.pwd = pwd;} @Overridepublic void Run () {HttpClient HttpClient = new Defaulthttpclient (); String url = "Http://192.168.1.103:8080/get01.jsp?name=" + name + "&pwd=" + pwd; HttpGet httpget = new HttpGet (URL); try {httpresponse response = Httpclient.execute (HttpGet); if (responsE.getstatusline (). Getstatuscode () = = () {httpentity entity = response.getentity (); BufferedReader reader = new BufferedReader (New InputStreamReader (Entity.getcontent ())); String result = Reader.readline (); LOG.D ("HTTP", "Get:" + result);}} catch (Exception e) {e.printstacktrace ();}}}}
Manifest
<uses-permission android:name= "Android.permission.INTERNET"/>


P.S:
first, post to submit data steps:
A structure request object;
---the data that needs to be passed to the server is placed in the key value pair object;
To place the prepared key-value pairs in the list;
-Generates an object representing the request body;
A list object that has a request key value pair is placed in the request body object;
To place the request body object in the Request object;
The request object is sent by the.

Second, build the local server:
Download tomcat, configure the path to the JDK for system variable java_home. such as: E:\Java\jdk1.8.0_20
-Run Startup.bat under the Tomcat/bin directory.
-3-win+r "cmd" ipconfig, learn the native IP address, enter the native IP address in the browser, and see if the configuration is successful.
In the tomcat/WebApps/root directory, create a new JSP page, get01.jsp.

<%    String name = Request.getparameter ("name");    String pwd = request.getparameter ("pwd");    Out.print ("Name:" + name + ", pwd:" + pwd);%>
Ok

Android_http Get and Post

Related Article

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.