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

热点内容
平安京编译器 浏览:869
把文件夹名称改成符号 浏览:128
iis如何启动命令 浏览:801
单片机驱动电机原理 浏览:905
app扫哈罗单车怎么操作 浏览:509
电脑版吃鸡为什么登录服务器忙 浏览:982
linux查端口命令 浏览:489
linuxhttp客户端 浏览:820
linux下运行jar包 浏览:435
彩虹弹弹解压球视频 浏览:83
pdf怎样转换成word格式 浏览:673
怎么查找解压文件在哪里 浏览:852
德语小说pdf 浏览:125
陕西联通dns服务器地址 浏览:939
js表格即时编译 浏览:304
51单片机串口拓展 浏览:307
重装系统后加密图片损坏 浏览:465
电脑怎么放大缩小app窗口 浏览:526
教育十APP学校怎么更改 浏览:823
空调外机压缩机热保护 浏览:756