Test
来源:互联网 发布:linux rm 删除文件 编辑:程序博客网 时间:2024/05/14 08:23
- package edu.whu.httpclientdemo;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- 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.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.util.EntityUtils;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener{
- Button bt1, bt2, bt3;
- TextView tv;
- HttpClient client;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- bt1 = (Button) findViewById(R.id.button1);
- bt2 = (Button) findViewById(R.id.button2);
- bt3 = (Button) findViewById(R.id.button3);
- tv = (TextView) findViewById(R.id.textView1);
- bt1.setOnClickListener(this);
- bt2.setOnClickListener(this);
- bt3.setOnClickListener(this);
- client = new DefaultHttpClient();// 创建HttpClient对象
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button1:// HttpClientGet请求网络
- new AsyncTask<String, Void, String>() {
- @Override
- protected String doInBackground(String... arg0) {
- String urlString = arg0[0];
- HttpGet get = new HttpGet(urlString);// 创建请求方法的实例
- try {
- HttpResponse response = client.execute(get);//获取连接响应信息
- // 可以调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头
- HttpEntity httpEntity = response.getEntity();// 获取HttpEntity实体
- // HttpEntity可以是流也可以是字符串,封装了一些对字符和流的操作,还有另外几种Entity实体类
- // String value = EntityUtils.toString( httpEntity
- // );//不加utf-8出现中文乱码
- String value = EntityUtils.toString(httpEntity, "utf-8");
- return value;
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- tv.setText(result);
- }
- }.execute("http://www.whu.edu.cn/index.htm");
- break;
- case R.id.button2:// HttpClientPost请求网络
- new AsyncTask<String, Void, String>() {
- @Override
- protected String doInBackground(String... arg0) {
- HttpPost post = new HttpPost(arg0[0]);// 创建请求方法的实例
- try {
- List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
- list.add(new BasicNameValuePair("name", "whu"));
- post.setEntity(new UrlEncodedFormEntity(list));// 设置请求参数
- } catch (UnsupportedEncodingException e1) {
- }
- try {
- HttpResponse response = client.execute(post);// 发送请求获取相应
- HttpEntity httpEntity = response.getEntity();// 获取HttpEntity实体
- // HttpEntity可以是流也可以是字符串,封装了一些对字符和流的操作,还有另外几种Entity实体类
- // String value = EntityUtils.toString( httpEntity
- // );//不加utf-8出现中文乱码
- String value = EntityUtils.toString(httpEntity, "utf-8");
- return value;
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- tv.setText(result);
- }
- }.execute("http://www.whu.edu.cn/index.htm");
- break;
- case R.id.button3:// 获取图片
- new AsyncTask<String, Void, Bitmap>() {
- @Override
- protected Bitmap doInBackground(String... params) {
- try {
- URL url = new URL(params[0]);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- //setConnectTimeout:设置连接主机超时(单位:毫秒)
- //setReadTimeout:设置从主机读取数据超时(单位:毫秒)
- conn.setReadTimeout(5 * 1000);//设置从主机读取超时时间为5秒
- int code = conn.getResponseCode();//获取请求码,不设置setReadTimeout可能阻塞线程
- if (code == 200) {//正常返回
- InputStream inputStream = conn.getInputStream();//获取字节流
- Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
- return bitmap;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(Bitmap result) {
- if (result != null) {
- tv.setBackground(new BitmapDrawable(result));
- }
- }
- }.execute("http://www.whu.edu.cn/images/2017021002.jpg");
- break;
- default:
- break;
- }
- }
- }
0 0
- test
- test
- test
- test
- test
- test
- test
- Test
- test
- Test
- test
- test
- test
- test
- test
- test
- test
- Test
- Java设计模式超详细
- 一篇关于后台与APP前端网络框架设计
- 【JavaScript】基础整理——函数调用相关
- 使用socket()函数创建套接字
- 生物医学研究之困
- Test
- 路由汇聚的计算方式
- Java编程思想之《初始化与清理》学习笔记
- 《C++ Primer》第9章:顺序容器
- Qt在Mac OS X下的编程环境搭建
- Windows下数据的接收和发送
- C++11多线程学习-1
- JAVAWEB开发之Spring详解之——Spring事务管理详解(四种事务管理方式)
- 关于JDBC的使用