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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值