首页 > 代码库 > 开源项目(asyncHttpClient) get post 方式提交
开源项目(asyncHttpClient) get post 方式提交
get方式:
public static void requestNetForGetLogin(final Context context,final Handler handler ,final String username, final String password) {
//使用HttpClient请求服务器将用户密码发送服务器验证
try{
String path = "http://192.168.13.83:8080/xuexi/servlet/LoginServlet?username="+URLEncoder.encode(username,"utf-8")+"&pwd="+URLEncoder.encode(password,"utf-8");
//创建一个AsyncHttpClient对象
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.get(path, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//statusCode:状态码 headers:头信息 responseBody:返回的内容,返回的实体
//判断状态码
if(statusCode == 200){
//获取结果
try {
String result = new String(responseBody,"utf-8");
Toast.makeText(context, result, 0).show();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
System.out.println("...............onFailure");
}
});
}catch (Exception e) {
e.printStackTrace();
}
}
post方式:
String path = "http://192.168.13.83:8080/xuexi/servlet/LoginServlet";
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("username", username);
params.put("pwd", password);
//url: parmas:请求时携带的参数信息 responseHandler:是一个匿名内部类接受成功过失败
asyncHttpClient.post(path, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//statusCode:状态码 headers:头信息 responseBody:返回的内容,返回的实体
//判断状态码
if(statusCode == 200){
//获取结果
try {
String result = new String(responseBody,"utf-8");
Toast.makeText(context, result, 0).show();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
}
开源项目(asyncHttpClient) get post 方式提交