1、首先添加AsyncHttpClient.jar包到libs文件夹下
2、初始化请求类以及响应回调类
private AsyncHttpClient client;
private AsyncHttpResponseHandler asyncHttpResponseHandler;
3、网络权限
<!-- 网络链接 -->
<uses-permission android:name="android.permission.INTERNET" />
4、实例化响应类
/*** 设置请求响应*/
private void setListener(final Context context) {asyncHttpResponseHandler = new AsyncHttpResponseHandler() {@Overridepublic void onSuccess(int i, Header[] headers, byte[] bytes) {String jsonStr = new String(bytes);Log.i("lgq","sss===="+jsonStr);}@Overridepublic void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {String jsonStr = new String(bytes);Log.i("lgq","sssbbb===="+jsonStr);}};
}
5、开始请求,添加了header请求参数和常用请求参数
注:如果是HttpURLConnection请求,添加header请求参数方法是:
conn.setRequestProperty("headkey","headervalue");
public void httpheader(){client = new AsyncHttpClient();client.addHeader("headkey", "22aa");//添加header请求参数方法RequestParams requestParams = new RequestParams();requestParams.add("password", "123");//添加常用请求参数方法String strUrl = "http://192.168.0.110:8080/servletDemo/testa?method=login&username=xzgkof&";client.post(this, strUrl, requestParams, asyncHttpResponseHandler);//开始请求
}
6、Android 请求结果:
02-22 11:09:19.492 21796-21796/com.tianxin.httpheader I/lgq: sss===={"msg":["登录成功!锤子进来"]}
二、附java响应实现代码
/**
* json格式化公用方法
* @param key
* @param list
* @return
*/
public JSONObject formatJsonData(String key,List<?> list){
JSONObject json = new JSONObject();
JSONArray ja = JSONArray.fromObject(list);
json.element(key, ja);
return json;
}
/**
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String usname = request.getParameter("username");//获取常用方法请求参数
String psword = request.getParameter("password");
System.out.println("hhhhhhhhhhhhhhhhh========"+request.getHeader("headkey"));//获取header请求参数
HttpSession session = request.getSession();
List<Object> msgList = new ArrayList<Object>();
// if (session.isNew()) {
// msgList.add("isNew==="+session.getId());
// }else {
// msgList.add("==="+session.getId());
// }
if(usname != null || psword != null){
if(session.getAttribute("usname") != null){
if(session.getAttribute("usname").equals(usname)
&&session.getAttribute("psword").equals(psword)){
okreString ="登录成功";
msgList.add("登录成功!上面水电费");
}else{
msgList.add("登录失败!用户名密码不可为空!");
okreString ="登录失败了";
}
}else{
if(usname.equals("xzgkof")&& psword.equals("123")){
msgList.add("登录成功!锤子进来");
session.setAttribute("usname", usname);
session.setAttribute("psword", psword);
session.setMaxInactiveInterval(30*60);//设置单位为秒,设置为-1永不过期
okreString ="登录成功111";
}else{
msgList.add("登录失败!用户名或密码错误!");
okreString ="登录失败了1111";
//session.setMaxInactiveInterval(1);
//request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}else{
msgList.add("用户名,密码不可为空!");
}
List<Object> msgList22 = new ArrayList<Object>();
msgList22.add("你的过第三方");
response.getWriter().print(formatJsonData("msg",msgList));
// response.getWriter().print(createJSONObject());
}
java获取header请求参数结果:
demo下载链接:https://download.csdn.net/download/meixi_android/10970252
demo云盘链接:https://pan.baidu.com/s/1wor8EZ7RjSddDK2Pw44C4g
云盘密码:回复QQ——1085220040