Ⅰ android post網路數據的請求
public static String HttpPostMethod(String get_url,
List<NameValuePair> params) {
String result = "";
HttpParams basicHttpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(basicHttpParams, 15 * 1000);
HttpConnectionParams.setSoTimeout(basicHttpParams, 15 * 1000);
HttpClient htc = new DefaultHttpClient(basicHttpParams);
HttpResponse httpResponse = null;
try {
HttpPost httpPost = new HttpPost(get_url);
// response = htc.execute(request);
// 設置httpPost請求參數
// 創建參數隊列
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");//json
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// httpResponse = new DefaultHttpClient().execute(httpPost);
httpResponse = htc.execute(httpPost);
// System.out.println(httpResponse.getStatusLine().getStatusCode());
Log.d("zh1", get_url+" " + httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 第三步,使用getEntity方法活得返回結果
result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
if(result==null||result.equals("")){
return null;
}
}else{
return null;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = null;
} catch (IOException e) {
e.printStackTrace();
result = null;
}
return result;
}
Ⅱ Android開發POST請求怎麼使用
網頁鏈接
這個博客表述的比較詳細的了,一般請求的話直接使用okhttp3這個庫就可以了。如果有大量介面的話還可以跟Retrofit2一起結合來使用。望採納。
Ⅲ 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();
}
Ⅳ 關於android post請求的問題
這個是網路請求不能用handler,必須新開一個線程,你可以在執行結果的位置用handler發送一個message,msg.obj攜帶結果。handler.sendmessage()。
handler需要在activity這樣寫:
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch(msg.what){
case 1:
//在這里刷新iew
break;
};
}
};
Ⅳ 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 = "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 = "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地址後面,例如/index.jsp?num=23&jjj=888; 但是這種形式對於那種比較隱私的參數是不適合的,而且參數的大小也是有限制的,一般是1K左右吧,對於上傳文件 就不是很適合。 post請求方式是將參數放在消息體內將其發送到伺服器,所以對大小沒有限制,對於隱私的內容也比較合適。 如下Post請求 POST /LoginCheck HTTP/1.1 Accept: text/html, application/xhtml+xml, */* Referer: 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 = "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; }
Ⅵ 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();
}
Ⅶ 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來獲取
Ⅷ Android的post請求該怎麼使用
post的參數包括:url(你請求的網站),date(發送的數據),header(所用的瀏覽器)這些都是常用的參數,更多的去查一下吧,我記得中軟國際某個視頻有講解。
Ⅸ android中怎麼發送post請求
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/lblPostResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/butPost"
android:layout_centerHorizontal="true"
android:text="提交結果" />
<Button
android:id="@+id/butPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:text="提交測試" />