安卓post get提交表单类
来源:互联网 发布:剑灵高冷正太捏脸数据 编辑:程序博客网 时间:2024/06/06 09:41
- */
- package com.jiuchongju.util;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.protocol.HTTP;
- import org.apache.http.util.EntityUtils;
- /**
- * Description:
- * @version 1.0
- */
- public class HttpUtil
- {
- // 创建HttpClient对象
- public static HttpClient httpClient ;
- public static final String BASE_URL =
- "http://www.xxx.com/projectname/name_baidu!jsonstring";
- /**
- *
- * @param url 发送请求的URL
- * @return 服务器响应字符串
- * @throws Exception
- */
- public static String getRequest(String url)
- throws Exception
- {
- httpClient= new DefaultHttpClient();
- try{
- // 创建HttpGet对象。
- HttpGet get = new HttpGet(url);
- // 发送GET请求
- HttpResponse httpResponse = httpClient.execute(get);
- // 如果服务器成功地返回响应
- if (httpResponse.getStatusLine()
- .getStatusCode() == 200)
- {
- // 获取服务器响应字符串
- String result = EntityUtils
- .toString(httpResponse.getEntity());
- return result;
- }
- }catch(Exception e){
- e.printStackTrace();
- return "获取数据失败!";
- }finally{
- httpClient.getConnectionManager().shutdown();
- }
- return null;
- }
- /**
- *
- * @param url 发送请求的URL
- * @param params 请求参数
- * @return 服务器响应字符串
- * @throws Exception
- */
- public static String postRequest(String url
- , Map<String ,String> rawParams)
- {
- httpClient= new DefaultHttpClient();
- try{
- // 创建HttpPost对象。
- HttpPost post = new HttpPost(url);
- // 如果传递参数个数比较多的话可以对传递的参数进行封装
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- for(String key : rawParams.keySet())
- {
- //封装请求参数
- params.add(new BasicNameValuePair(key , rawParams.get(key)));
- }
- // 设置请求参数
- post.setEntity(new UrlEncodedFormEntity(
- params,HTTP.UTF_8));
- // 发送POST请求
- HttpResponse httpResponse = httpClient.execute(post);
- // 如果服务器成功地返回响应
- if (httpResponse.getStatusLine()
- .getStatusCode() == 200)
- {
- // 获取服务器响应字符串
- String result = EntityUtils
- .toString(httpResponse.getEntity());
- return result;
- }
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- httpClient.getConnectionManager().shutdown();
- }
- return null;
- }
- }
- */
- package com.jiuchongju.util;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.protocol.HTTP;
- import org.apache.http.util.EntityUtils;
- /**
- * Description:
- * @version 1.0
- */
- public class HttpUtil
- {
- // 创建HttpClient对象
- public static HttpClient httpClient ;
- public static final String BASE_URL =
- "http://www.xxx.com/projectname/name_baidu!jsonstring";
- /**
- *
- * @param url 发送请求的URL
- * @return 服务器响应字符串
- * @throws Exception
- */
- public static String getRequest(String url)
- throws Exception
- {
- httpClient= new DefaultHttpClient();
- try{
- // 创建HttpGet对象。
- HttpGet get = new HttpGet(url);
- // 发送GET请求
- HttpResponse httpResponse = httpClient.execute(get);
- // 如果服务器成功地返回响应
- if (httpResponse.getStatusLine()
- .getStatusCode() == 200)
- {
- // 获取服务器响应字符串
- String result = EntityUtils
- .toString(httpResponse.getEntity());
- return result;
- }
- }catch(Exception e){
- e.printStackTrace();
- return "获取数据失败!";
- }finally{
- httpClient.getConnectionManager().shutdown();
- }
- return null;
- }
- /**
- *
- * @param url 发送请求的URL
- * @param params 请求参数
- * @return 服务器响应字符串
- * @throws Exception
- */
- public static String postRequest(String url
- , Map<String ,String> rawParams)
- {
- httpClient= new DefaultHttpClient();
- try{
- // 创建HttpPost对象。
- HttpPost post = new HttpPost(url);
- // 如果传递参数个数比较多的话可以对传递的参数进行封装
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- for(String key : rawParams.keySet())
- {
- //封装请求参数
- params.add(new BasicNameValuePair(key , rawParams.get(key)));
- }
- // 设置请求参数
- post.setEntity(new UrlEncodedFormEntity(
- params,HTTP.UTF_8));
- // 发送POST请求
- HttpResponse httpResponse = httpClient.execute(post);
- // 如果服务器成功地返回响应
- if (httpResponse.getStatusLine()
- .getStatusCode() == 200)
- {
- // 获取服务器响应字符串
- String result = EntityUtils
- .toString(httpResponse.getEntity());
- return result;
- }
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- httpClient.getConnectionManager().shutdown();
- }
- return null;
- }
- }
0 0
- 安卓post get提交表单类
- python get,post提交表单
- python get,post提交表单
- 表单提交get和post
- 安卓利用get和post方式提交数据
- 安卓基础-get post请求与提交数据
- 深入研究表单提交方式:GET/POST
- 表单提交方法post和get区别
- 表单的提交方式POST和GET
- post 和 get 提交表单是的异同
- POST与GET提交表单的区别
- POST与GET提交表单的区别
- 表单提交的post 和get
- 表单提交post与get方法
- asp.net表单提交方法GET/POST
- 表单提交 get 与post的区别
- 深入研究表单提交方式:GET/POST
- C#.net模拟提交表单GET、POST
- iOS 根据文本内容为TextView动态定义高度
- 《iOS 7 Programming Pushing the Limits》系列:你可能不知道的ObjC技
- 跟我学设计设计模式视频教程(一)-设计模式概述&单例模式
- NPAPI插件开发学习:插件中多个dll文件的打包
- iOS被忽略的main函数
- 安卓post get提交表单类
- PAT循环-09. 支票面额(15)
- Hadoop Reducer个数设置
- Hadoop中Partition的定制
- C++内存池
- 静态初始化,非静态初始化
- 梦想黑马程序员__夜雨飘零的心绪
- NPAPI插件开发学习:NPAPI的运行流程
- 浅谈msp430f5529入门(1)——时钟模块DCO