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
原创粉丝点击