post上傳數組
『壹』 post發送JSON數據(字元串、數組、字典、自定義對象)給伺服器
post發送JSON數據(字元串、數組、字典、自定義對象)給伺服器
觸發發送的方法
這次Demo是通過點擊屏幕觸發發送數據給伺服器事件
前提需要開啟本地模擬伺服器
發送JSON字元串
發送字典給伺服器
發送數組給伺服器
發送oc對象給伺服器
先講對象轉換為字典
通過系統提供的JSON解析類進行序列化
覺得不錯請點贊支持,歡迎留言或進我的個人群855801563領取【架構資料專題目合集90期】、【BATJTMD大廠JAVA面試真題1000+】,本群專用於學習交流技術、分享面試機會,拒絕廣告,我也會在群內不定期答題、探討
『貳』 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; }
『叄』 python的urllib如何POST傳遞數組參數
1.如果機器上安裝了 Python 的 setuptools,可以通過下面的命令來安裝 poster:
easy_install poster
# test_client.pyfrom poster.encode import multipart_encodefrom poster.streaminghttp import register_openersimport urllib2# 在 urllib2 上注冊 http 流處理句柄register_openers()# 開始對文件 "DSC0001.jpg" 的 multiart/form-data 編碼# "image1" 是參數的名字,一般通過 HTML 中的 <input> 標簽的 name 參數設置# headers 包含必須的 Content-Type 和 Content-Length# datagen 是一個生成器對象,返回編碼過後的參數datagen, headers = multipart_encode({"image1": open("DSC0001.jpg", "rb")})# 創建請求對象request = urllib2.Request("http://localhost:5000/upload_image", datagen, headers)# 實際執行請求並取得返回print urllib2.urlopen(request).read()
很簡單,文件就上傳完成了。
2.其中那個 register_openers() 相當於以下操作:
from poster.encode import multipart_encodefrom poster.streaminghttp import StreamingHTTPHandler, StreamingHTTPRedirectHandler, StreamingHTTPSHandlerhandlers = [StreamingHTTPHandler, StreamingHTTPRedirectHandler, StreamingHTTPSHandler]opener = urllib2.build_opener(*handlers)urllib2.install_opener(opener)
3.另外,poster 也可以攜帶 cookie,比如:
opener = poster.streaminghttp.register_openers()opener.add_handler(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))params = {'file': open("test.txt", "rb"), 'name': 'upload test'}datagen, headers = poster.encode.multipart_encode(params)request = urllib2.Request(upload_url, datagen, headers)result = urllib2.urlopen(request)