1. android post請求和get請求的區別
Get請求與Post請求的區別
Get是向伺服器發索取數據的一種請求,而Post是向伺服器提交數據的一種請求
Get是獲取信息,而不是修改信息,類似資料庫查詢功能一樣,數據不會被修改
Get請求的參數會跟在url後進行傳遞,請求的數據會附在URL之後,以?分割URL和傳輸數據,參數之間以&相連,%XX中的XX為該符號以16進製表示的ASCII,如果數據是英文字母/數字,原樣發送,如果是空格,轉換為+,如果是中文/其他字元,則直接把字元串用BASE64加密。
Get傳輸的數據有大小限制,因為GET是通過URL提交數據,那麼GET可提交的數據量就跟URL的長度有直接關系了,不同的瀏覽器對URL的長度的限制是不同的。
GET請求的數據會被瀏覽器緩存起來,用戶名和密碼將明文出現在URL上,其他人可以查到歷史瀏覽記錄,數據不太安全。在伺服器端,用Request.QueryString來獲取Get方式提交來的數據
Post請求則作為http消息的實際內容發送給web伺服器,數據放置在HTML Header內提交,Post沒有限制提交的數據。Post比Get安全,當數據是中文或者不敏感的數據,則用get,因為使用get,參數會顯示在地址,對於敏感數據和不是中文字元的數據,則用post
POST表示可能修改變伺服器上的資源的請求,在伺服器端,用Post方式提交的數據只能用Request.Form來獲取
2. android post請求參數怎麼寫
一、需要用到的場景 在jQuery中使用$.post()就可以方便的發起一個post請求,在android程序中有時也要從伺服器獲取一些數據,就也必須得使用post請求了。 二、需要用到的主要類 在android中使用post請求主要要用到的類是HttpPost、HttpResponse
3. android中post請求怎麼傳輸內容
一、需要用到的場景
在jQuery中使用$.post()就可以方便的發起一個post請求,在android程序中有時也要從伺服器獲取一些數據,就也必須得使用post請求了。
二、需要用到的主要類
在android中使用post請求主要要用到的類是HttpPost、HttpResponse、EntityUtils
三、主要思路
1、創建HttpPost實例,設置需要請求伺服器的url。
2、為創建的HttpPost實例設置參數,參數設置時使用鍵值對的方式用到NameValuePair類。
3、發起post請求獲取返回實例HttpResponse
4、使用EntityUtils對返回值的實體進行處理(可以取得返回的字元串,也可以取得返回的byte數組)
代碼也比較簡單,注釋也加上了,就直接貼出來了
[java]
package com.justsy.url;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
public class HttpURLActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("start url...");
String url = "http://192.168.2.112:8080/JustsyApp/Applet";
// 第一步,創建HttpPost對象
HttpPost httpPost = new HttpPost(url);
// 設置HTTP POST請求參數必須用NameValuePair對象
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("action", "downloadAndroidApp"));
params.add(new BasicNameValuePair("packageId", "89dcb664-50a7-4bf2-aeed-49c08af6a58a"));
params.add(new BasicNameValuePair("uuid", "test_ok1"));
HttpResponse httpResponse = null;
try {
// 設置httpPost請求參數
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
httpResponse = new DefaultHttpClient().execute(httpPost);
//System.out.println(httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 第三步,使用getEntity方法活得返回結果
String result = EntityUtils.toString(httpResponse.getEntity());
System.out.println("result:" + result);
T.displayToast(HttpURLActivity.this, "result:" + result);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end url...");
setContentView(R.layout.main);
}
}
ADD:使用HttpURLConnection 進行post請求
[java]
String path = "http://192.168.2.115:8080/android-web-server/httpConnectServlet.do?PackageID=89dcb664-50a7-4bf2-aeed-49c08af6a58a";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
System.out.println(conn.getResponseCode());
============================================================================================================================
通過get和post方式向伺服器發送請求
首先說一下get和post的區別
get請求方式是將提交的參數拼接在url地址後面,例如http://www..com/index.jsp?num=23&jjj=888;
但是這種形式對於那種比較隱私的參數是不適合的,而且參數的大小也是有限制的,一般是1K左右吧,對於上傳文件
就不是很適合。
post請求方式是將參數放在消息體內將其發送到伺服器,所以對大小沒有限制,對於隱私的內容也比較合適。
如下Post請求
POST /LoginCheck HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://192.168.2.1/login.asp
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: 192.168.2.1
Content-Length: 39
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: language=en
Username=admin&checkEn=0&Password=admin //參數位置
在android中用get方式向伺服器提交請求:
在android模擬器中訪問本機中的tomcat伺服器時,注意:不能寫localhost,因為模擬器是一個單獨的手機系統,所以要寫真是的IP地址。
否則無法訪問到伺服器。
//要訪問的伺服器地址,下面的代碼是要向伺服器提交用戶名和密碼,提交時中文先要經過URLEncoder編碼,因為模擬器默認的編碼格式是utf-8
//而tomcat內部默認的編碼格式是ISO8859-1,所以先將參數進行編碼,再向伺服器提交。
private String address = "http://192.168.2.101:80/server/loginServlet";
public boolean get(String username, String password) throws Exception {
username = URLEncoder.encode(username);// 中文數據需要經過URL編碼
password = URLEncoder.encode(password);
String params = "username=" + username + "&password=" + password;
//將參數拼接在URl地址後面
URL url = new URL(address + "?" + params);
//通過url地址打開連接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//設置超時時間
conn.setConnectTimeout(3000);
//設置請求方式
conn.setRequestMethod("GET");
//如果返回的狀態碼是200,則一切Ok,連接成功。
return conn.getResponseCode() == 200;
}
在android中通過post方式提交數據。
public boolean post(String username, String password) throws Exception {
username = URLEncoder.encode(username);// 中文數據需要經過URL編碼
password = URLEncoder.encode(password);
String params = "username=" + username + "&password=" + password;
byte[] data = params.getBytes();
URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
//這是請求方式為POST
conn.setRequestMethod("POST");
//設置post請求必要的請求頭
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 請求頭, 必須設置
conn.setRequestProperty("Content-Length", data.length + "");// 注意是位元組長度, 不是字元長度
conn.setDoOutput(true);// 准備寫出
conn.getOutputStream().write(data);// 寫出數據
return conn.getResponseCode() == 200;
}
4. 一個安卓問題,請問安卓post提交數據到伺服器,可以傳遞一個實體類對象給伺服器嗎,裡面帶有參數,
數據統一使用json格式傳輸,你的實體類轉換成json後再往伺服器傳
5. android get/post如何實現多參數請求
可使用android自帶的httpclient框架實現。
GET 方式傳遞參數
//先將參數放入List,再對參數進行URL編碼
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "數據")); //增加參數1
params.add(new BasicNameValuePair("param2", "value2"));//增加參數2
String param = URLEncodedUtils.format(params, "UTF-8");//對參數編碼
String baseUrl = "伺服器介面完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//將URL與參數拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //發起GET請求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//獲取伺服器響應內容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2.POST方式 方式傳遞參數
//和GET方式一樣,先將參數放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加參數1
params.add(new BasicNameValuePair("param2", "第二個參數"));//增加參數2
try {
HttpPost postMethod = new HttpPost(baseUrl);//創建一個post請求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //將參數填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //執行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //獲取響應內容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
6. android 如何調用默認瀏覽器(webservice)打開網頁使用post的方式傳遞參數。
用HttpURLConnection對象,我們可以向網路發送請求參數. String requestUrl = "http://localhost:8080/itcast/contanctmanage.do"; Map<String, String> requestParams = new HashMap<String, String>(); requestParams.put("age", "12"); requestParams.put("name", "中國"); StringBuilder params = new StringBuilder(); for(Map.Entry<String, String> entry : requestParams.entrySet()){ params.append(entry.getKey()); params.append("="); params.append(URLEncoder.encode(entry.getValue(), "UTF-8")); params.append("&"); } if (params.length() > 0) params.deleteCharAt(params.length() - 1); byte[] data = params.toString().getBytes(); URL realUrl = new URL(requestUrl); HttpURLC...
7. 安卓 post json數據值怎麼傳遞數組
在開發Android、iOS等移動應用時經常需要以JSON的方式向伺服器傳輸圖片或者文字。如何實現呢,常見的方法是將圖片或文件轉換為字元串,如二進制的01字元串進,行傳輸,當伺服器接收到字元串後對其進行解析,並將...
8. post請求參數傳數組
public String androidPost() { String rs = null; String path = "url/Android_JDBC_SH/AndroidLoginAction"; HttpPost hp = new HttpPost(path); //獲取客戶端,用來向伺服器發出請求 DefaultHttpClient hc = new DefaultHttpClient(); try { //Default Constructor taking a name and a value BasicNameValuePair nm = new BasicNameValuePair("name", name); BasicNameValuePair pa = new BasicNameValuePair("password", password); List list = new ArrayList(); list.add(nm); list.add(pa); //構建向伺服器發送的實體 HttpEntity entity = new UrlEncodedFormEntity(list); hp.setEntity(entity); //提交請求,獲取伺服器的響應 HttpResponse response = hc.execute(hp); if (response.getStatusLine().getStatusCode() == 200) { //獲取響應實體 entity = response.getEntity(); rs = EntityUtils.toString(entity); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return rs; }
9. android開發中,公司需求,使用post請求,但是請求參數需要丟到請求頭裡面去。
我舉個例子給你
把所有參數放進一個map,然後傳過去
public interface BlueService {
@GET("book/search")
Call<BookSearchResponse> getSearchBooks(@QueryMap Map<String, String> options);
}
Map<String, String> options = new HashMap<>();
map.put("q", "小王子");
map.put("tag", null);
map.put("start", "0");
map.put("count", "3");
Call<BookSearchResponse> call = mBlueService.getSearchBooks(options);
10. android 怎麼發送post請求並接收二進制數據
可使用android自帶的httpclient框架實現向伺服器發起get或post請求,以下為完整的示例代碼:
1. GET 方式傳遞參數
//先將參數放入List,再對參數進行URL編碼
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "數據")); //增加參數1
params.add(new BasicNameValuePair("param2", "value2"));//增加參數2
String param = URLEncodedUtils.format(params, "UTF-8");//對參數編碼
String baseUrl = "伺服器介面完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//將URL與參數拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //發起GET請求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//獲取伺服器響應內容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2. POST方式 方式傳遞參數
//和GET方式一樣,先將參數放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加參數1
params.add(new BasicNameValuePair("param2", "第二個參數"));//增加參數2
try {
HttpPost postMethod = new HttpPost(baseUrl);//創建一個post請求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //將參數填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //執行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //獲取響應內容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}