Android的HttpConnection和HttpClient发送请求
来源:互联网 发布:淘宝卖家级别 编辑:程序博客网 时间:2024/06/05 00:25
package com.mine.networktest;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;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 android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener { public static final int SHOW_RESPONSE = 0; public static final int SHOW_HttpClient = 0; private Button sendRequest_btn; private Button httpClient_btn; private TextView responseTest; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case SHOW_RESPONSE: String response = (String) msg.obj; // 在这里进行UI操作,将结果显示到界面上 responseTest.setText(response); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sendRequest_btn = (Button) findViewById(R.id.send_request); httpClient_btn = (Button)findViewById(R.id.http_client); responseTest = (TextView) findViewById(R.id.response_text); sendRequest_btn.setOnClickListener(this); httpClient_btn.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId() ) { case R.id.send_request: sendRequestWithHttpURLConnection(); break; case R.id.http_client: sendRequestWithHttpClient(); break; default: break; } } private void sendRequestWithHttpClient() { // TODO Auto-generated method stub new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://www.baidu.com"); try { HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode()==200) { //请求和响应都成功了 HttpEntity entity = httpResponse.getEntity(); String response = EntityUtils.toString(entity, "utf-8"); Message msg = new Message(); msg.what = SHOW_RESPONSE; //将服务器返回的结果放在Message中 msg.obj = response.toString(); handler.sendMessage(msg); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } private void sendRequestWithHttpURLConnection() { // 开启线程发送网络请求 new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; try { URL url = new URL("http://www.haidu.com"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); InputStream is = connection.getInputStream(); // 对获取到的输入流进行读取 BufferedReader reader = new BufferedReader( new InputStreamReader(is)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } Message msg = new Message(); msg.what = SHOW_RESPONSE; // 将服务器返回的结果保存在message中 msg.obj = response.toString(); handler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } } } }).start(); }}
0 0
- Android的HttpConnection和HttpClient发送请求
- httpClient和httpconnection的区别
- httpClient和httpconnection的区别
- android中HttpClient和HttpConnection的区别和应用
- android中HttpClient和HttpConnection的区别和应用
- Android HttpConnection HttpClient
- Android HTTP(HttpClient, HttpConnection)
- HttpConnection的post请求发送数据,接收方出现乱码
- HttpConnection的get请求
- Android中使用HttpURLConnection和HttpClient发送Http请求
- Android笔记---使用HttpClient发送POST和GET请求
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- 【实战】(二)android模拟qq登录(apache的httpClient发送get和post请求)
- 我的Android进阶之旅------>Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- 我的Android进阶之旅------>Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- [Socket]利用Android下的HttpClient发送GET && POST请求
- 指针的初始化
- sed 简明教程
- 0000_0000_0000_0101_pnchart-swift出现crash解决
- UDP、TCP、RTP三种协议的总结
- MySQL性能比较测试:MySQL5.6 GA -VS- MySQL5.5
- Android的HttpConnection和HttpClient发送请求
- Maven的优点
- 程序员技术练级攻略
- u盘安装ubuntu14.04
- 浅谈struts2之chain
- Aerospike C客户端手册——Redhat/CentOS 6 安装
- Web网页开发心得笔记
- 所有OC 面试题 就是这么简单。真的边边教你 学ios
- Java基础面试题——(6)