標籤:android http get post
record。
1、Http的請求種類:
Get :通過請求URL得到資源
Post :用於向伺服器提交新的內容
Put :用於修改某個內容
Delete :用於刪除某個內容
Options :用於查看伺服器的效能
Trace :用於遠端診斷伺服器
.......
在漫長的時間當中,其他的方法逐漸的退出了曆史舞台,最常用的只剩下GET和POST方法。
2、Get與Post方法的區別
Get方法就好比一張明信片,只有要求標頭。而Post方法就好比一封信,有要求標頭和請求體。
-1-Get方法(等冪方法)用於從伺服器取回資料,Post方法用於向伺服器提交資料。
-2-使用Get方法向伺服器提交的資料量較小,通常不超過2K;使用Post向伺服器提交的資料量通常沒有限制。
-3-Get請求是將所要提交的資料附在URL之後,而Post請求是將提交的資料放置在請求的請求體當中。
Get請求範例:http://192.168.1.100:8081/s02e14.jsp?name=away&pwd=123456
3、使用Get與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);pwdText = (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();//使用POST方法向伺服器發送請求PostThread pt = new PostThread(name, pwd);pt.start();//使用GET方法向伺服器發送請求//GetThread gt = new GetThread(name, pwd);//gt.start();}});}// 該線程使用Post方法向伺服器發送請求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";//產生使用POST方法的請求對象HttpPost httpPost = new HttpPost(url);// NameValuePair對象代表了一個需要發往伺服器的索引值對NameValuePair pair1 = new BasicNameValuePair("name", name);NameValuePair pair2 = new BasicNameValuePair("pwd", pwd);//將準備好的索引值對對象放置在一個List當中ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();pairs.add(pair1);pairs.add(pair2);//建立代表請求體的對象try {HttpEntity requestEntity = new UrlEncodedFormEntity(pairs);//將請求體放置在請求對象當中httpPost.setEntity(requestEntity);//執行請求對象try {HttpResponse response = httpClient.execute(httpPost);if (response.getStatusLine().getStatusCode() == 200) {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();}}}// 該線程使用Get方法向伺服器發送請求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() == 200) {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:
一、Post提交資料的步驟:
-1-構造請求對象;
-2-將需要傳遞給伺服器端的資料放置在鍵值對對象當中;
-3-將準備好的鍵值對放置在List當中;
-4-產生代表請求體的對象;
-5-將存有請求鍵值對的List對象放置在請求體對象當中;
-6-將請求體對象放置在請求對象當中;
-7-發送請求對象。
二、搭建本機伺服器:
-1-下載Tomcat,配置系統變數JAVA_HOME的jdk的路徑。如:E:\Java\jdk1.8.0_20
-2-運行tomcat/bin目錄下的startup.bat。
-3-win+r》cmd》ipconfig,獲知本機ip地址,在瀏覽器輸入本機ip地址,看看配置是否成功。
-4-在tomcat/webapps/ROOT目錄下,建立一個jsp頁面,get01.jsp。
<% String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); out.print("name:" + name + ",pwd:" + pwd);%>OK、
android_Http Get與Post