導航:首頁 > 操作系統 > androidhttppost文件

androidhttppost文件

發布時間:2022-06-04 02:20:00

android通過http post實現文件下載

可參照我的如下代碼

java">java.io.OutputStreamos=null;
java.io.InputStreamis=null;
try{
java.io.Filefile=newjava.io.File(str_local_file_path);
if(file.exists()&&file.length()>0){
}else{
file.createNewFile();

java.net.URLurl=newjava.net.URL(str_url);
java.net.HttpURLConnectionconn=(java.net.HttpURLConnection)url.openConnection();
os=newjava.io.FileOutputStream(file);
is=conn.getInputStream();
byte[]buffer=newbyte[1024*4];
intn_rx=0;
while((n_rx=is.read(buffer))>0){
os.write(buffer,0,n_rx);
}
}
returntrue;
}catch(MalformedURLExceptione){
}catch(IOExceptione){
}finally{
os.flush();
os.close();
is.close();
}
returnfalse;

⑵ 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 Studio用httpPost向伺服器傳json數據,StringEntity不存在,求高手幫忙

你最後具體怎麼解決?

⑷ 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();
}

⑸ 安卓開發 http中get和post的區別

get就是要傳遞的參數在url里,
比如http://xxx.com?username=bcoder
post就是參數放在數據包里,用戶不能直接看到,相對安全些

閱讀全文

與androidhttppost文件相關的資料

熱點內容
農機空氣壓縮機 瀏覽:664
程序員下載歌曲 瀏覽:894
編譯未檢測到模擬器 瀏覽:807
壓縮機每次啟動12分鍾就停 瀏覽:730
creo復制曲面命令 瀏覽:959
程序員戀上女碩士 瀏覽:669
ansys的get命令 瀏覽:988
國外dns蘋果伺服器地址 瀏覽:430
國家職業技術資格證書程序員 瀏覽:652
奇瑞租車app是什麼 瀏覽:99
系統源碼安裝說明 瀏覽:420
命令行加殼 瀏覽:96
解壓時顯示防失效視頻已加密 瀏覽:295
蘋果簡訊加密發送 瀏覽:446
天翼私有雲伺服器租用 瀏覽:733
貴州雲伺服器屬於哪個上市公司 瀏覽:58
編程聯動教程 瀏覽:482
小天才app怎麼升級v242 瀏覽:545
簡單手工解壓玩具製作大全 瀏覽:928
免費編程電子書 瀏覽:870