Android网络连接----使用HttpClient连接网络

package com.test.shiweiwe.networkapp;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

/**
 * Created by Administrator on 2015/9/13.
 */
public class MyHTTPClient extends Activity implements View.OnClickListener {
    private Button mButtonDoPost;
    private Button mButtonDoGet;
    private TextView mTextView;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x23:
                    String message = (String) msg.obj;
                    mTextView.setText(message);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_httpclient);
        mTextView = (TextView) findViewById(R.id.textview_httpclient);
        mButtonDoGet = (Button) findViewById(R.id.button_httpclient_doget);
        mButtonDoPost = (Button) findViewById(R.id.button_httpclient_dopost);
        mButtonDoGet.setOnClickListener(this);
        mButtonDoPost.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_httpclient_dopost:

                break;
            case R.id.button_connect_net_doget:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        httpClientDoGet();
                    }
                }).start();

                break;
        }

    }

    private void httpClientDoGet() {
        //得到url
//        String urlString = "http://192.168.0.55:8088/WebTest/ServletTest?name=zhangsan&password=123456";
        String urlString = "http://www.baidu.com";
        HttpClient client = new DefaultHttpClient();
        //设置get方法
        HttpGet get = new HttpGet(urlString);

        try {
            //执行get方法得到服务器返回的所有数据都在response中
            HttpResponse response = client.execute(get);
            //httpClient访问服务器返回的标头,包含http状态码
            StatusLine status = response.getStatusLine();
            int code = status.getStatusCode();
            if (code == HttpURLConnection.HTTP_OK) {
                //得到数据的实体
                HttpEntity entity = response.getEntity();
                String line = EntityUtils.toString(entity, "UTF-8");
                Message message = new Message();
                message.what = 0x23;
                message.obj = line;
                handler.sendMessage(message);
                Log.d("httpClientDoGet", line);

            }
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`jbundle-util-osgi-wrapped-httpclient` 是一个用于在 OSGi(开放服务网关倡议)环境中使用 Apache HttpClient 的工具库。它提供了将 Apache HttpClient 封装为 OSGi 插件的功能,使得在 OSGi 容器中可以方便地使用 HttpClient 进行 HTTP 请求和处理。 Apache HttpClient 是一个功能强大的 HTTP 客户端库,用于在 Java 应用程序中进行 HTTP 通信。它提供了丰富的功能和灵活的配置选项,包括支持各种 HTTP 方法、处理认证和代理、处理 Cookie、支持连接池等。 使用 `jbundle-util-osgi-wrapped-httpclient` 可以进行以下操作: 1. 封装 Apache HttpClient:`jbundle-util-osgi-wrapped-httpclient` 将 Apache HttpClient 封装为 OSGi 插件,使得它可以在 OSGi 容器中进行动态加载和管理。这样你就可以通过 OSGi 的方式来使用 HttpClient,而无需手动处理其依赖和生命周期管理。 2. OSGi 配置和服务:工具库提供了一些实用方法和类,用于在 OSGi 环境中配置和使用 HttpClient。它提供了 OSGi 配置管理器和服务注册的支持,使得你可以方便地配置和获取 HttpClient 实例。 3. HTTP 请求和响应处理:`jbundle-util-osgi-wrapped-httpclient` 提供了一些工具类和方法,用于发送 HTTP 请求和处理响应。你可以使用这些工具来构建和发送 HTTP 请求,并处理服务器返回的响应数据。 需要注意的是,`jbundle-util-osgi-wrapped-httpclient` 是一个针对 OSGi 环境的工具库,主要用于在 OSGi 容器中使用 Apache HttpClient。如果你不使用 OSGi,可以直接使用 Apache HttpClient 的原生库。 希望这个解释对你有帮助!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值