安卓基础:使用HttpClient访问网络
2016-03-08 21:41
567 查看
之前介绍了使用HttpURLConnection类来访问网路,在一般情况下,如果只需要到某个简单的页面提交请求并获取服务器的响应,完全可以使用该类技术实现。不过对于比较复杂的联网操作,使用HttpURLConnection类就不一定能满足要求,这时可以使用Apache组织提供的HttpClient项目来实现,在Android中集成了HttpClient,所以可以直接使用HttpClient来访问网络。接下来就是实例:
1.创建HttpCLientTest类,里面包含了get请求和post请求的方法;
2.XML文件与之前的一样
3.MainActivity中跟之前的也差不多
4.现象也与之前的一样
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.现象也与之前的一样
相关文章推荐
- TCP、UDP和HTTP详解
- 开源网络监控管理系统:OpenNMS
- 云数据中心网络虚拟化——大二层技术巡礼之NVo3技术端到端隧道
- Tcp/IP的理解-socket
- TCP流量控制与拥塞控制
- 网络七层 以及iOS开发中的get请求 post请求
- 【转】水煮TCPMP
- TCPMP的ARM编译器问题
- HTTP中Get与Post的区别
- HTTP Status 404 - No result defined for action XXXX and result success 处理
- android studio碰到无法找到org.apache.http.legacy的解决办法
- HTTP 协议
- 转: HTTP Live Streaming直播(iOS直播)技术分析与实现
- iOS开发之HTTP、TCP/IP、Socket
- ThinkPHP3.2版本提交HTTP返回302错误问题
- BP神经网络的自适应步长问题
- HttpClient4.5教程-第一章-基础 1.2-1.8
- 移动平台游戏网络重连方案
- Retrofit 代替 HttpRequest
- 弱网络下的游戏服务器设计