WebApi HttpUtils
public class HttpUtils {
private static final String TAG = "uploadFile";
private static final int TIME_OUT = * ;//超时时间
/**
* 通过GET方式发送请求
*
* @param url URL地址
* @param params 参数
*/
public static String httpGet(String url, String params) {
//返回信息
String response = null;
try {
//拼接请求地址
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(url);
if (null != params && !params.equals("")) urlBuilder.append("?" + params);
// 构造HttpClient的实例
HttpClient httpClient = new DefaultHttpClient();
// 创建GET方法的实例
HttpGet getMethod = new HttpGet(urlBuilder.toString());
//设置请求APIToken和请求方式
getMethod.addHeader("Accept-Language", WebApiUrl.Accept_Language);
getMethod.addHeader("Authorization", "Basic " + WebApiUrl.Authorization);
getMethod.addHeader("Content-Type", WebApiUrl.Content_Type);
//获取HTTP请求响应结果
HttpResponse httpResponse = httpClient.execute(getMethod);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) //SC_OK = 200
{
// 获得返回结果
response = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception ex) {
Log.i(TAG, String.format("HttpGet请求发生异常:{0}", ex.toString()));
}
return response;
}
/**
* 通过POST方式发送请求
*
* @param url URL地址
* @param params 参数
*/
public static String httpPost(String url, String params) {
String result = null;
try {
StringEntity entity = new StringEntity(params, "utf-8");
entity.setContentType("application/json");
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
httpPost.addHeader("Authorization", "Basic " + WebApiUrl.Authorization);
// 构造HttpClient的实例
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
result = EntityUtils.toString(responseEntity, "UTF-8");
} catch (Exception ex) {
Log.i(TAG, String.format("HttpPost请求发生异常:{0}", ex.toString()));
}
return result;
}
}
Post请求案例
实例一:接口入参为对象
public ManagementBagRsult ManagementDetainedBags(DetentionBagParam param) {
Gson os=new Gson();
String paramStr=os.toJson(param);
try {
String result = HttpUtils.httpPost(WebApiUrl.ManagementDetainedBags_Url,paramStr);
ManagementBagRsult info = os.fromJson(result, ManagementBagRsult.class);
return info;
} catch (Exception ex) {
Log.i("TAG", String.format("ScanBagInfoContractImpl/ManagementDetainedBags:{0}", ex.toString()));
return null;
}
}
实例二:接口入参为String单个属性
public class LoginContractImpl implements ILoginContract {
private static final Logger logger = LoggerFactory.getLogger();
@Override
public ApLoginResult doLogin(DetentionBagParam param) {
try {
//传入参数,获得结果集
String result = HttpUtils.httpPost(WebApiUrl.Login_Url+"?OrganizationCode="+param.OrganizationCode+"&UserWorkNumber="+param.UserWorkNumber+"&UserPassWord="+param.UserPassWord, "");
//序列化一个泛型对象
Gson gs = new Gson();
ApLoginResult info = gs.fromJson(result, ApLoginResult.class);
return info;
} catch (Exception ex) {
logger.info(String.format("LoginContractCompl发生异常:{0}", ex.toString()));
return null;
}
}
}
实例三:GET请求
public Boolean SetLocactionIsFull(String locaton_code) {
String params="location_code="+locaton_code;
try {
String result=HttpUtils.httpGet(WebApiUrl.SetLocactionIsFull_Url, params);
//获取对应的值
if (result == null) return false;
//反序列化一个泛型对象
Gson gs = new Gson();
Type jsonType = new TypeToken<RequestModel.Request<Boolean>>() {}.getType();
RequestModel.Request<Boolean> res= gs.fromJson(result, jsonType);
if(res!=null && res.ResultCode.equals("") && res.ResultDesc.equals("接口调用成功")){
return res.Item;
}
} catch (Exception ex) {
logger.info(String.format("{0}/{1}发生异常:{2}", "QuickShelfContractImpl", "InStoresOperating", ex.toString()));
}
return false;
}
WebApi HttpUtils的更多相关文章
- webapi - 使用依赖注入
本篇将要和大家分享的是webapi中如何使用依赖注入,依赖注入这个东西在接口中常用,实际工作中也用的比较频繁,因此这里分享两种在api中依赖注入的方式Ninject和Unity:由于快过年这段时间打算 ...
- ASP.NET Core MVC/WebAPi 模型绑定探索
前言 相信一直关注我的园友都知道,我写的博文都没有特别枯燥理论性的东西,主要是当每开启一门新的技术之旅时,刚开始就直接去看底层实现原理,第一会感觉索然无味,第二也不明白到底为何要这样做,所以只有当你用 ...
- Asp.Net WebApi核心对象解析(下篇)
在接着写Asp.Net WebApi核心对象解析(下篇)之前,还是一如既往的扯扯淡,元旦刚过,整个人还是处于晕的状态,一大早就来处理系统BUG,简直是坑爹(好在没让我元旦赶过来该BUG),队友挖的坑, ...
- Taurus.MVC 2.2 开源发布:WebAPI 功能增强(请求跨域及Json转换)
背景: 1:有用户反馈了关于跨域请求的问题. 2:有用户反馈了参数获取的问题. 3:JsonHelper的增强. 在综合上面的条件下,有了2.2版本的更新,也因此写了此文. 开源地址: https:/ ...
- Taurus.MVC 2.0 开源发布:WebAPI开发教程
背景: 有用户反映,Tausus.MVC 能写WebAPI么? 能! 教程呢? 嗯,木有! 好吧,刚好2.0出来,就带上WEBAPI教程了! 开源地址: https://github.com/cyq1 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(64)-补充WebApi与Unity注入-配置文件
系列目录 上一篇演示了WebApi利用Unity注入 很多人问我如何用配置文件来配置注入,本节演示如何利用配置文件来注入,道理是一样的,跳转到上一节下载源码一起来动手! 1.打开源码定位到文件Depe ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(66)-MVC WebApi 用户验证 (2)
系列目录 前言: 回顾上一节,我们利用webapi简单的登录并进行了同域访问与跨域访问来获得Token,您可以跳转到上一节下载代码来一起动手. 继续上一篇的文章,我们接下来演示利用拿到的Token来访 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(65)-MVC WebApi 用户验证 (1)
系列目录 前言: WebAPI主要开放数据给手机APP,其他需要得知数据的系统,或者软件应用,所以移动端与系统的数据源往往是相通的. Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能, ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(64)-WebApi与Unity注入
系列目录 前言: 有时候我们系统需要开放数据给手机App端或其他移动设备,不得不说Asp.net WebApi是目前首选 本节记录Asp.net MVC WebApi怎么利用Unity注入.系列开头已 ...
随机推荐
- Educational Codeforces Round 72 (Rated for Div. 2)
https://www.cnblogs.com/31415926535x/p/11601964.html 这场只做了前四道,,感觉学到的东西也很多,,最后两道数据结构的题没有补... A. Creat ...
- CreateFolder
import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.apac ...
- Unity - 存读档机制简析
本文旨在于简要分析Unity中的两种存档机制,即:PlayerPrefs数据持久化方法及Serialization数据序列化方法 较比与源项目,我另加了JSON方法.XML方法等及一些Unity设置, ...
- MYSQL-用户密码修改
解决方法如下:1.终端中结束当前正在运行的mysql进程.# sudo /etc/init.d/mysql stop2.用mysql安全模式运行并跳过权限验证.# sudo /usr/bin/mysq ...
- Shell之Xargs命令
目录 Shell之Xargs命令 参考 xargs命令简介 xargs命令格式 xargs实例说明 Shell之Xargs命令
- 列表 元祖 range
1.列表 list 存放一些数据的容器 比如 衣柜 书包 作用:存储一些数据,数据量比较大 可以下标 可以切片 可以步长 和字符串的完全一样 lst = [1,2,3] print(lst) #[1, ...
- Openshift 部署第一个应用hello-openshift
Openshift 部署第一个应用hello-openshift: cd /opt/ wget https://github.com/openshift/origin/releases/downloa ...
- JAVA错误提示:The operation is not applicable to the current selection.Select a field which is not declared as type variable or a type that declares such fields.
平时没怎么注意,今天用Eclipse自动生成Set Get方法时提示错误,错误信息如下: The operation is not applicable to the current selectio ...
- 索引的底层实现(B 树)
一.B 树 1.B-Tree介绍 B-树的搜索,从根结点开始,对结点内的关键字(有序)序列进行二分查找,如果命中则结束,否则进入查询关键字所属范围的儿子结点:重复,直到所对应的儿子指针为空,或已经是叶 ...
- Redis Sentinel(哨兵核心机制) 初步深入
##### 1.Redis 的 Sentinel 系统用于管理多个 Redis 服务 该系统执行以下三个任务: 1.监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务 ...