您的位置:首页 > 理论基础 > 计算机网络

使用AsyncHttpClient进行get与post请求

2018-03-06 21:30 555 查看
本来我们可以使用HttpURLConnection来完成get与post请求,但是Android中有自己的请求方式,就是HttpClient,关键代码如下:

先封装一下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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息