Android http协议的get和post的区别
来源:互联网 发布:ftp使用的端口 编辑:程序博客网 时间:2024/06/11 06:53
一、get请求;
void GetDate() {
// URL
String uriAPI = "http://172.0.0.0:8082/TestServelt/login";
String idString = "110";
String nameString = "cyy";
String url = uriAPI + "?" + "id=" + idString + "&name=" + nameString;
HttpGet get = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);// 执行Post方法
String resultString = EntityUtils.toString(response.getEntity());//得到结果
} catch (Exception e) {
}
}
二、post请求:
方式一;httppost
void GetDate() {
// URL
String uriAPI = "http://172.0.0.0:8082/TestServelt/login";
String idString = "110";
String nameString = "cyy";
String url = uriAPI + "?" + "id=" + idString + "&name=" + nameString;
HttpGet get = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);// 执行Post方法
String resultString = EntityUtils.toString(response.getEntity());//得到结果
} catch (Exception e) {
}
}
方式二:HttpURLConnection
void GetDate() {
// URL
String uriAPI = "http://172.0.0.0:8082/TestServelt/login";
String idString = "110";
String nameString = "cyy";
String url = uriAPI + "?" + "id=" + idString + "&name=" + nameString;
HttpGet get = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);// 执行Post方法
String resultString = EntityUtils.toString(response.getEntity());//得到结果
} catch (Exception e) {
}
}
二、post请求:
方式一;httppost
void GetDate() {
// URL
String uriAPI = "http://172.0.0.0:8082/TestServelt/login";
String idString = "110";
String nameString = "cyy";
String url = uriAPI + "?" + "id=" + idString + "&name=" + nameString;
HttpGet get = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);// 执行Post方法
String resultString = EntityUtils.toString(response.getEntity());//得到结果
} catch (Exception e) {
}
}
方式二:HttpURLConnection
- class AsyncHttpURL extends AsyncTask<Object, Object, String> {
- @Override
- protected String doInBackground(Object... params) {
- try {
- // 建立连接
- URL url = new URL(uriAPI);
- HttpURLConnection httpConn = (HttpURLConnection) url
- .openConnection();
- httpConn.setConnectTimeout(5 * 1000);
- httpConn.setReadTimeout(10 * 1000);
- // //设置连接属性
- // 设置是否向connection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true
- httpConn.setDoOutput(true);
- // 使用 URL 连接进行输入, Default is true.
- httpConn.setDoInput(true);
- // Post 请求不能使用缓存,忽略缓存
- httpConn.setUseCaches(false);
- // 是成员函数,仅作用于当前函数
- httpConn.setInstanceFollowRedirects(true);
- // 设置URL请求方法, Default is GET
- httpConn.setRequestMethod("POST");
- // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
- // 意思是正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode进行编码
- httpConn.setRequestProperty("Content-Type",
- "application/x-www-form-urlencoded;charset=UTF-8");
- // 编码方式,上面设置了charset=UTF-8,这里就不用设置了
- httpConn.setRequestProperty("Charset", "UTF-8");
- // 维持长连接
- httpConn.setRequestProperty("Connection", "Keep-Alive");
- // 要注意的是connection.getOutputStream会隐含的进行connect。
- httpConn.connect();
- DataOutputStream out = new DataOutputStream(
- httpConn.getOutputStream());
- // The URL-encoded contend
- // 正文,正文内容其实跟get的URL中'?'后的参数字符串一致,传多参请看下面方法
- String content = "name="+ URLEncoder.encode("cyy", "utf-8");
- // DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面
- out.writeBytes(content);
- out.flush();
- out.close();
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(httpConn.getInputStream()));
- StringBuilder sb = new StringBuilder();
- String lineTxt = null;
- while ((lineTxt = reader.readLine()) != null) {
- sb.append(lineTxt);
- }
- reader.close();
- httpConn.disconnect();
- return sb.toString();
- } catch (Exception ex) {
- ex.printStackTrace();
- return ex.toString();
- }
- }
- @Override
- protected void onPostExecute(String result) {
- tv.setText(result);
- }
- }
- NameValuePair转换String方法:
- /** NameValuePair转换String */
- private String getReqString(List<? extends NameValuePair> params) {
- // 正文,正文内容其实跟get的URL中'?'后的参数字符串一致
- StringBuffer req = new StringBuffer("");
- if (null != params && !params.isEmpty()) {
- NameValuePair pair;
- pair = params.get(0);
- try {
- req.append(pair.getName() + "="
- + URLEncoder.encode(pair.getValue(), "utf-8"));
- for (int i = 1; i < params.size(); i++) {
- pair = params.get(i);
- req.append("&" + pair.getName() + "="
- + URLEncoder.encode(pair.getValue(), "utf-8"));
- }
- } catch (UnsupportedEncodingException e) {
- }
- }
- return req.toString();
- }
阅读全文
0 0
- Android http协议的get和post的区别
- HTTP协议GET和POST的区别
- Http协议的get和post区别
- HTTP协议 GET和POST的区别
- HTTP协议中 POST和GET的区别和注意事项
- http协议的get和post方式的区别
- HTTP协议的POST和GET的区别
- http协议的get和post方法的区别
- HTTP 协议中的GET 和POST 的区别
- HTTP协议中 POST和GET的区别
- http协议中get和post的区别
- HTTP 协议请求方法get和post的区别
- HTTP协议中 POST和GET的区别
- Http协议中 get和post的区别
- HTTP协议中POST和GET的区别
- http协议中get和post的区别(转)
- HTTP协议中 POST和GET的区别
- HTTP协议中GET、POST和HEAD的区别?
- HttpReqeust对象
- 《程序员健康指南》读书笔记
- 深入浅出LDA(2)
- 大数据早报:谷歌在印度孟买建首个云服务平台 MariaDB完成C轮2700万美元融资(11.3)
- VedioCapture类介绍
- Android http协议的get和post的区别
- 从职场新人到企业高管,她是怎么做到的?
- 解决PHP date调用警告:It is not safe to rely on the system's timezone settings 转载
- QueryDict对象
- 剑指offer:序列化二叉树
- Java 类加载机制详解
- 隐写:NTFS STREAM
- IntelliJ IDEA 快捷键
- Cannot build artifact 'baseserver:war exploded' because it is included into a circular dependency