androidpost數組
❶ android post請求json參數list認證怎樣實現
如果採用post請求,與後台傳送參數採用json格式,那麼可以採用如下的形式包裝參數:
JSONObject params = new JSONObject();
params.put("signature",signature);
params.put("timestamp",timestamp);
params.put("nouce",nouce);
params.put("parnter",parnter);
params.put("access_token",access_token);
包裝之後可以採用一個訪問網路的工具類HttpClient訪問後台介面就可以了
我不知道你說的是不是這個意思,希望幫到你
❷ 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; }
❸ 安卓開發過程中怎樣看到post請求的數據結構
一、需要用到的場景 在jQuery中使用$.post()就可以方便的發起一個post請求,在android程序中有時也要從伺服器獲取一些數據,就也必須得使用post請求了。 二、需要用到的主要類 在android中使用post請求主要要用到的類是HttpPost、HttpResponse
❹ 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; }
❺ php伺服器端怎樣接收來自android的json數據.android以post方式發送
php有一個函數叫json_encode,數據從伺服器中拿過來之後,我是直接添加進array裡面來進行操作的,android認的JSONObject的格式是兩層大括弧包著的array。 你將數據從資料庫中拿出來之後,組成associative array,用你的例子創建一個空array先~~ $arr = array(); $arr['test'] = 'json'; $arr['mode'] = 'single'; 這樣加進一個叫$arr的數組(中文是叫這個的吧。。。orz。。。。)之後,你用另一個array再把它裝進去,操作是 $arr2 = array('view' => $arr); 這樣我們要的那個主要的包含數據的數組$arr就有了一個名字,於是android解析的時候就可以區別了,php輸出的時候,要這樣輸出: echo json_encode($arr2); 於是就ok~~~會變成一個可以解析的JSONObject哦~~~~ 以上全部是我個人研究經驗。。。。也許有更簡單的方法,求高手指教~~~不過我們整個一個系統裡面凡是server和android軟體交互的數據我都是這么發過去的,表示JSONArray是更麻煩的東西,JSONObject神馬的,還是很簡單的哈~~~~~自己研究研究就出來了~~~
❻ Android開發怎麼用xutilspost數組到後台
HttpUtils http = new HttpUtils();
RequestParams params = new RequestParams();
// params.addHeader("Content-Type", "application/json");
params.addBodyParameter("tel", tel);
params.addBodyParameter("password", password);
params.addBodyParameter("code", code);
http.send(HttpRequest.HttpMethod.POST, url,new RequestCallBack<String>() {
@Override
public void onFailure(HttpException error, String msg) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
// TODO Auto-generated method stub
System.out.println(responseInfo.result);
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("請求路徑===" + this.getRequestUrl());
}
);
❼ 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;
}
❽ 安卓 post json數據值怎麼傳遞數組
在開發Android、iOS等移動應用時經常需要以JSON的方式向伺服器傳輸圖片或者文字。如何實現呢,常見的方法是將圖片或文件轉換為字元串,如二進制的01字元串進,行傳輸,當伺服器接收到字元串後對其進行解析,並將...
❾ Java web中怎樣取得Android通過post發送的json數據
一. JSON的數據格式 a) 按照最簡單的形式,可以用下面這樣的 JSON 表示名稱/值對: { "firstName": "Brett" } b) 可以創建包含多個名稱/值對的記錄,比如: { "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance中國" } c) 可以創建值的數組 { "people": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance中國" }, { "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets中國" } ]} d) 當然,可以使用相同的語法表示多個值(每個值包含多個記錄): { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance中國" }, { "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets中國" } ], "authors": [ { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" } ], "musicians": [ { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" } ] } 注意,在不同的主條目(programmers、authors 和 musicians)之間,記錄中實際的名稱/值對可以不一樣。JSON 是完全動態的,允許在 JSON 結構的中間改變表示數據的方式。 二. 在 JavaScript 中使用 JSON JSON 是 JavaScript 原生格式,這意味著在 JavaScript 中處理 JSON 數據不需要任何特殊的 API 或工具包。 二.一 將 JSON 數據賦值給變數 例如,可以創建一個新的 JavaScript 變數,然後將 JSON 格式的數據字元串直接賦值給它: var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance中國" }, { "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets中國" } ], "authors": [ { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" } ], "musicians": [ { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" } ] } 二.二 訪問數據 將這個數組放進 JavaScript 變數之後,就可以很輕松地訪問它。實際上,只需用點號表示法來表示數組元素。所以,要想訪問 programmers 列表的第一個條目的姓氏,只需在JavaScript 中使用下面這樣的代碼: people.programmers[0].lastName; 注意,數組索引是從零開始的。 二.三 修改 JSON 數據 正如訪問數據,可以按照同樣的方式修改數據: people.musicians[一].lastName = "Rachmaninov"; 二.四 轉換回字元串 a) 在 JavaScript 中這種轉換也很簡單: String newJSONtext = people.toJSONString(); b) 可以將任何 JavaScript 對象轉換為 JSON 文本。並非只能處理原來用 JSON 字元串賦值的變數。為了對名為 myObject 的對象進行轉換,只需執行相同形式的命令: String myObjectInJSON = myObject.toJSONString(); 說明:將轉換回的字元串作為Ajax調用的字元串,完成非同步傳輸。 小結:如果要處理大量 JavaScript 對象,那麼 JSON 幾乎肯定是一個好選擇,這樣就可以輕松地將數據轉換為可以在請求中發送給伺服器端程序的格式。 三. 伺服器端的 JSON 三.一 將 JSON 發給伺服器 a) 通過 GET 以名稱/值對發送 JSON 在 JSON 數據中會有空格和各種字元,Web 瀏覽器往往要嘗試對其繼續編譯。要確保這些字元不會在伺服器上(或者在將數據發送給伺服器的過程中)引起混亂,需要在JavaScript的escape()函數中做如下添加: var url = "organizePeople.php?people=" + escape(people.toJSONString()); request.open("GET", url, true); request.onreadystatechange = updatePage; request.send(null); b) 利用 POST 請求發送 JSON 數據 當決定使用 POST 請求將 JSON 數據發送給伺服器時,並不需要對代碼進行大量更改,如下所示: var url = "organizePeople.php?timeStamp=" + new Date().getTime(); request.open("POST", url, true); request.onreadystatechange = updatePage; request.setRequestHeader("Content-Type", "application/x-至美-form-urlencoded"); request.send(people.toJSONString()); 注意:賦值時格式必須是var msg=eval('(' + req.responseText + ')'); 三.二 在伺服器上解釋 JSON a) 處理 JSON 的兩步驟。 針對編寫伺服器端程序所用的語言,找到相應的 JSON 解析器/工具箱/幫助器 API。 使用 JSON 解析器/工具箱/幫助器 API 取得來自客戶機的請求數據並將數據轉變成腳本能理解的東西。 b) 尋找 JSON 解析器 尋找 JSON 解析器或工具箱最好的資源是 JSON 站點。如果使用的是 Java servlet,json.org 上的 org.json 包就是個不錯的選擇。在這種情況下,可以從 JSON Web 站點下載 json.zip 並將其中包含的源文件添加到項目構建目錄。編譯完這些文件後,一切就就緒了。對於所支持的其他語言,同樣可以使用相同的步驟;使用何種語言取決於您對該語言的中國程度,最好使用您所熟悉的語言。 c) 使用 JSON 解析器 一旦獲得了程序可用的資源,剩下的事就是找到合適的方法進行調用。如果在 servlet 中使用的是 org.json 包,則會使用如下代碼: public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { StringBuffer jb = new StringBuffer(); String line = null; try { BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) jb.append(line); } catch (Exception e) { //report an error } try { JSONObject jsonObject = new JSONObject(jb.toString()); } catch (ParseException e) { // crash and burn throw new IOException("Error parsing JSON request string"); } // Work with the data using methods like... // int someInt = jsonObject.getInt("intParamName"); // String someString = jsonObject.getString("stringParamName"); // JSONObject nestedObj = jsonObject.getJSONObject("nestedObjName"); // JSONArray arr = jsonObject.getJSONArray("arrayParamName"); // etc...
❿ 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();
}
