HttpClient使用方式
来源:互联网 发布:usb电流检测软件 编辑:程序博客网 时间:2024/05/21 12:20
使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。
1. 创建HttpClient对象。
2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
5.调用HttpResponse的getEntity()方法返回一个HttpEntity对象
6.调用HttpEntity对象的getContent()方法可以获得一个InputStream对象,该对象就是通过http获取到的页面的内容。
代码示例:
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;public class MainActivity extends AppCompatActivity { private TextView txt; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { Bundle bundle=msg.getData(); if (bundle!=null) { String result=bundle.getString("result"); txt.setText(result); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //实例化HttpClient对象 HttpGet对象 txt = (TextView) findViewById(R.id.txt); new Thread(runnable).start(); } /** * 必须写在子线程中,还需要在build.gradle中加 * android { * useLibrary 'org.apache.http.legacy' * } */ public Runnable runnable = new Runnable() { @Override public void run() { HttpClient httpClient = new DefaultHttpClient(); //都改用HttpURLConnection HttpGet httpGet = new HttpGet("http://www.apress.com/book/view/9781430232674"); HttpResponse response=null; try { response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream is = entity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } String result = sb.toString(); Bundle bundle = new Bundle(); bundle.putString("result", result); Message message = handler.obtainMessage(); message.setData(bundle); message.what = 0x01; handler.sendMessage(message); is.close(); } } catch (IOException e) { e.printStackTrace(); } } };}<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.day705.MainActivity"><ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" /></ScrollView></RelativeLayout>
0 0
- HttpClient使用方式
- 使用HttpClient方式请求网络
- 使用HttpClient方式请求网络
- HttpClient工具正确使用方式
- HttpClient工具正确使用方式
- 使用HttpClient的POST方式
- HttpClient-----3、使用HttpClient进行GET方式通信
- HttpClient-----4、使用HttpClient进行POST方式通信
- 使用httpclient utf8方式请求数据
- (Apache)使用HttpClient方式访问HTTP
- 使用HttpClient发送https请求GET方式
- 使用HttpClient的GET方式下载数据
- 使用httpclient提交表单方式上传文件
- 使用HttpURLConnection和使用HttpClient方式请求网络采用get方式和post方式请求数据
- HttpClient使用GET方式通过代理服务器读取页面的例子
- HttpClient使用GET方式通过代理服务器读取页面的例子
- JAVA 使用httpclient、XFire方式调用Web Service的方法
- HttpClient 使用 java -- POST方式带参提交数据
- VC++中设置内存“块号”断点,检测内存泄漏问题
- Autumn中文文档2:控制器
- 【远程调用框架】如何实现一个简单的RPC框架(五)优化三:软负载中心设计与实现
- java常见的排序算法
- OkHttp 的详细介绍
- HttpClient使用方式
- Linux资料流重导向
- 线程池/安全停止线程
- sftp常用命令介绍
- Python链接oracle数据库(上)
- 查看 Git Clone文件下载在哪里
- nswoj-回文数
- eclipse 导入maven项目
- mybatis分页插件