导航:首页 > 操作系统 > 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文件相关的资料

热点内容
苹果id怎么验证app 浏览:862
查看手机命令 浏览:951
抖音反编译地址 浏览:223
如何加密软件oppoa5 浏览:231
java从入门到精通明日科技 浏览:92
拆解汽车解压视频 浏览:595
新版百度云解压缩 浏览:591
android上下拉刷新 浏览:878
centos可执行文件反编译 浏览:836
林清玄pdf 浏览:270
黑马程序员java基础 浏览:283
awss3命令 浏览:358
百度店铺客户订单手机加密 浏览:501
钉钉班群文件夹怎么上传文件 浏览:749
人社app怎么解绑手机 浏览:101
caj文件夹打不开 浏览:475
什么app可以将电量变色 浏览:692
解放出你的解压抖音小游戏 浏览:346
什么方式解压比较好 浏览:267
erp是什么服务器 浏览:186