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

安卓基础:使用HttpClient访问网络

2016-03-08 21:41 567 查看
之前介绍了使用HttpURLConnection类来访问网路,在一般情况下,如果只需要到某个简单的页面提交请求并获取服务器的响应,完全可以使用该类技术实现。不过对于比较复杂的联网操作,使用HttpURLConnection类就不一定能满足要求,这时可以使用Apache组织提供的HttpClient项目来实现,在Android中集成了HttpClient,所以可以直接使用HttpClient来访问网络。接下来就是实例:

1.创建HttpCLientTest类,里面包含了get请求和post请求的方法;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
* 发送Get和Post请求的方法类
*/
public class HttpClientThread extends Thread{

private String url;
public HttpClientThread(String url) {
this.url = url;
}

private String name;
private String age;
public HttpClientThread(String url, String name, String age) {
this.url = url;
this.name = name;
this.age = age;
}

/**
* 发送get请求的方法
* 1.创建HttpClient对象。
* 2.创建HttpGet对象。
* 3.如果要发送请求参数,可以直接将要发送的参数连接到URL地址中,
*   也可以调用HttpGet的setParams()方法来添加请求参数
* 4.调用HttpClient对象的execute() 方法发送请求。执行该方法将返回一个HttpResponse对象。
* 5.调用HttpResponse的getEntity()方法,可获得包含服务器响应内容的HttpEntity对象,
*   通过该对象可以获得服务器的响应内容
*/
private void doHttpClientGet(){
//创建HttpClient对象,所有的发送都是通过HttpClient发送的
HttpClient httpClient = new DefaultHttpClient();
//创建HttpGet连接对象,把url传递进去,故发送请求
HttpGet httpGet = new HttpGet(url);
HttpResponse response;
try {
//通过execute()方法发送get请求
response = httpClient.execute(httpGet);
//如果服务器返回的结果码没有什么问题
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
//得到服务器返回的数据
String content = EntityUtils.toString(response.getEntity());
//在客户端把content打印出来
System.out.println("content------>"+content);
}
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 发送Post请求的方法
* 同使用HttpURLConnection类发送请求一样,对于复杂的请求数据,也需要使用POST方式发送。
* 使用步骤:
* 1.创建HttpClient对象。
* 2.创建HttpPost对象。
* 3.如果需要发送请求参数,可以调用HttpPost的setParams()方法来添加请求参数,
*   也可以调用setEntity()方法来设置请求参数。
* 4.调用HttpClient对象的execute()方法发送请求。执行该方法将返回一个HttpResponse对象。
* 5.调用HttpResponse的getEntity()方法,克获得包含服务器响应内容的HttpEntity对象,
*   通过该对象可以获得服务器的响应内容。
*/
public void doHttpClientPost(){
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
//将要传递的参数保存到List集合中,通过NameValuePair存储数据
ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
//添加数据
list.add(new BasicNameValuePair("name",name));
list.add(new BasicNameValuePair("age",age));
try {
//设置要发送的数据
post.setEntity(new UrlEncodedFormEntity(list));
HttpResponse response = client.execute(post);

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
//如果请求成功,获取返回的字符串
String content = EntityUtils.toString(response.getEntity());
};
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

//在run()方法中访问doHttpClientGet()或doHttpClientPost()方法;
@Override
public void run() {
//doHttpClientGet();
doHttpClientPost();
}
}


2.XML文件与之前的一样

3.MainActivity中跟之前的也差不多

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

private EditText name;
private EditText age;
private Button regist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.name);
age = (EditText) findViewById(R.id.age);
regist = (Button) findViewById(R.id.regist);

regist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "http://192.168.173.1:8080/web/MyServlet";
String na = name.getText().toString();
String ag = age.getText().toString();

//用get请求时使用此
//url = url+"?name="+na+"&age="+ag;
//new HttpClientThread(url).start();

//用户post请求使用此
new HttpClientThread(url,na,ag).start();

}
});
}
}


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