Android网络连接----使用HttpClient连接网络
2015-09-14 21:36
441 查看
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(); } } }
相关文章推荐
- Android网络连接----使用URLConnection实现从服务器上下载
- Android第九讲——网络(一)
- (4.4.12)Android开发:如何实现TCP和UDP传输
- URLConnection/Android Stutio中HttpClient的添加jar包方法和DoGet、DoPost的用法
- Android网络连接----使用URLConnection连接网络简单例子
- (5.2.3) 写给那些让我糊里糊涂的HTTP、TCP、UDP、Socket
- Httpclient
- n-1位数(http://acm.nyist.net/JudgeOnline/problem.php?pid=96)
- 网络连接之——xUtils 介绍(三)
- Android0912<十七>(Android 网络URLConnection)(ing多线程下载)
- Android网络连接---xUtils(HttpUtils)
- Android网络技术
- hdu 5438 Ponds(长春网络赛 拓扑+bfs)
- 第三十八天 网络连接:Volley、xUtils
- Java网络编程之Socket的使用
- HDU 5442 Favorite Donut (2015年长春赛区网络赛F题)
- Android网络连接---Google网络库Volley
- 超文本传输协议,http请求过程
- android HttpClient的使用
- HDU 5438 Ponds(2015ACM长春网络赛+枚举删点+DFS求联通块)