使用AsyncHttpClient进行get与post请求
2018-03-06 21:30
555 查看
本来我们可以使用HttpURLConnection来完成get与post请求,但是Android中有自己的请求方式,就是HttpClient,关键代码如下:
先封装一下Toast以便查看数据:
get请求:
post请求:
而且好多都是过时的方法,使用AsyncHttpClient开源项目的话就会很简洁:
get请求:
post请求:
附上此开源项目的百度网盘下载地址:链接:https://pan.baidu.com/s/1pMM2ncZ 密码:lx9g
模拟登陆的被请求的Servlet:
先封装一下Toast以便查看数据:
private void myToast(final String context) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), context, Toast.LENGTH_SHORT).show(); } }); }
get请求:
//获取Httpclient实例 DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); //定义get请求 HttpGet httpGet = new HttpGet(path); //执行get请求 HttpResponse httpResponse = defaultHttpClient.execute(httpGet); //获取状态码 int code = httpResponse.getStatusLine().getStatusCode(); if(code==200){ myToast(IOUtils.toString(httpResponse.getEntity().getContent())); }
post请求:
//获取Httpclient实例 DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); //定义post请求 HttpPost httpPost = new HttpPost(path); List<NameValuePair> array = new ArrayList<>(); BasicNameValuePair basic_name = new BasicNameValuePair("username", usernameString); BasicNameValuePair basic_pwd = new BasicNameValuePair("password", passwordString); array.add(basic_name); array.add(basic_pwd); UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(array); httpPost.setEntity(urlEncodedFormEntity); HttpResponse httpResponse = defaultHttpClient.execute(httpPost); int code = httpResponse.getStatusLine().getStatusCode(); if (code == 200) { InputStream stream = httpResponse.getEntity().getContent(); myToast(IOUtils.toString(stream)); }
而且好多都是过时的方法,使用AsyncHttpClient开源项目的话就会很简洁:
get请求:
//使用开源项目进行get方式提交 AsyncHttpClient asyncHttpClient_get = new AsyncHttpClient(); asyncHttpClient_get.get(path, new AsyncHttpResponseHandler() { //请求成功的方法回调 public void onSuccess(int arg0,Header[] arg1, byte[] arg2) { Toast.makeText(getApplicationContext(), new String(arg2), 1).show(); }; //请求失败的方法回调 public void onFailure(int arg0,Header[] arg1, byte[] arg2, Throwable arg3) { Toast.makeText(getApplicationContext(), new String(arg2), Toast.LENGTH_SHORT).show(); }; });
post请求:
//使用开源项目进行get方式提交 AsyncHttpClient asyncHttpClient_post = new AsyncHttpClient(); //封装提交的数据 RequestParams requestParams = new RequestParams(); requestParams.put("username", username_post_str); requestParams.put("password", password_post_str); //开始Post请求 asyncHttpClient_post.post(path1, requestParams, new AsyncHttpResponseHandler() { @Override public void onSuccess(int arg0, Header[] arg1, byte[] arg2) { Toast.makeText(getApplicationContext(), new String(arg2),Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) { Toast.makeText(getApplicationContext(), new String(arg2), Toast.LENGTH_LONG).show(); } });
附上此开源项目的百度网盘下载地址:链接:https://pan.baidu.com/s/1pMM2ncZ 密码:lx9g
模拟登陆的被请求的Servlet:
package com.xpu.apptest; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Login extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); String strusername = new String(username.getBytes("iso-8859-1"),"utf-8"); String struserpwd = new String(password.getBytes("iso-8859-1"),"utf-8"); System.out.println(request.getMethod()+"请求,用户名:"+strusername+" 密码:"+struserpwd); if("abc".equals(username)&&"123".equals(password)){ response.getOutputStream().write("成功".getBytes("UTF-8")); }else{ response.getOutputStream().write("失败".getBytes("UTF-8")); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
相关文章推荐
- android(21)(使用HttpURLConnection,HttpClient和AsyncHttpClient分别实现post和get请求数据)
- Java:使用HttpClient进行POST和GET请求以及文件上传和下载
- Java:使用HttpClient进行POST和GET请求以及文件上传和下载
- Java:使用HttpClient进行POST和GET请求以及文件上传和下载
- 使用AsyncHttpClient 实现get和post请求
- 使用AsyncHttpClient 实现get和post请求
- 使用开源框架AsyncHttpClient进行Json的Post请求
- 使用HttpClient进行get和post请求
- 使用HttpClient进行POST和GET请求以及文件上传和下载
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- 使用httpclient发送get或post请求
- 一个使用HttpClient get post请求的实例
- 使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml
- java使用httpclient模拟post请求和get请求示例
- 使用httpClient处理get请求或post请求
- java使用httpclient封装post请求和get的请求
- java使用httpclient封装post请求和get的请求
- [置顶] 使用Apache POI 进行Excel报表的导出下载(GET 请求 && POST请求)
- nodejs 使用http进行post或get请求(携带cookie)
- 使用POST、GET、AsyncHttpClient创造服务端用手机端来连接进行登录