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

Android_访问网络二(使用HttpClient访问)

2014-09-01 17:37 218 查看
PS:看了9年的小说,自己开始动手写了一本,请各位猿们动动手指,点击下,有起点账号的可以收藏下!!《武意长存》

上一篇介绍完了如何使用HttpURLConnection访问网络,接下来我们介绍如何使用HttpCilent进行网络的访问

public class LoginService {
private static final String BASE_PATH = "http://110.89.138.70:8080/server/LoginServlet";

public static String loginByClientGet(String username, String password){
//定义一个客户端
HttpClient client = new DefaultHttpClient();

String path = null;
try {
path = BASE_PATH + "?username="+URLEncoder.encode(username, "utf-8")+"&password="+URLEncoder.encode(password, "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}

// 定义一个get请求方法
HttpGet httpGet = new HttpGet(path);

HttpResponse httpResponse = null;
try {
httpResponse = client.execute(httpGet);
// 获得响应码
int code = httpResponse.getStatusLine().getStatusCode();
if(code == 200){
InputStream is = httpResponse.getEntity().getContent();
return StreamTools.readInputStream(is);
}else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

public static String loginByClientPost(String username, String password){
//定义一个客户端
HttpClient client = new DefaultHttpClient();
// 定义post方法
HttpPost httpPost = new HttpPost(BASE_PATH);

//指定要提交的数据实体
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", username));
parameters.add(new BasicNameValuePair("password", password));

HttpResponse httpResponse = null;
try {
//以utf-8对参数进行编码
httpPost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));

httpResponse = client.execute(httpPost);
int code = httpResponse.getStatusLine().getStatusCode();
if(code == 200){
InputStream is = httpResponse.getEntity().getContent();
return StreamTools.readInputStream(is);
}else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}

return null;
}
}


StreamTools.java、服务端的代码以及乱码问题可参见上一篇文章



点击loginByClientGet按钮调用以下方法

public void loginByClientGet(View view){
final String username = etUsername.getText().toString().trim();
final String password = etPassword.getText().toString().trim();

if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
Toast.makeText(this, "用户名或密码不能为空!", Toast.LENGTH_SHORT).show();
}else {
new Thread(){
public void run() {
final String result = LoginService.loginByClientGet(username, password);
if(null != result){	//请求成功
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
}
});
}else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
}
});
}
};
}.start();
}
}


点击loginByClientPost按钮调用以下方法

public void loginByClientPost(View view){
final String username = etUsername.getText().toString().trim();
final String password = etPassword.getText().toString().trim();

if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
Toast.makeText(this, "用户名或密码不能为空!", Toast.LENGTH_SHORT).show();
}else {
new Thread(){
public void run() {
final String result = LoginService.loginByClientPost(username, password);
if(null != result){	//请求成功
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
}
});
}else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
}
});
}
};
}.start();
}


账号和密码正确时

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