Test

来源:互联网 发布:linux rm 删除文件 编辑:程序博客网 时间:2024/05/14 08:23
  1. package edu.whu.httpclientdemo;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.graphics.drawable.BitmapDrawable;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11. import org.apache.http.HttpEntity;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.client.ClientProtocolException;
  14. import org.apache.http.client.HttpClient;
  15. import org.apache.http.client.entity.UrlEncodedFormEntity;
  16. import org.apache.http.client.methods.HttpGet;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.impl.client.DefaultHttpClient;
  19. import org.apache.http.message.BasicNameValuePair;
  20. import org.apache.http.util.EntityUtils;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.UnsupportedEncodingException;
  24. import java.net.HttpURLConnection;
  25. import java.net.URL;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  29. Button bt1, bt2, bt3;
  30. TextView tv;
  31. HttpClient client;
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.activity_main);
  36. bt1 = (Button) findViewById(R.id.button1);
  37. bt2 = (Button) findViewById(R.id.button2);
  38. bt3 = (Button) findViewById(R.id.button3);
  39. tv = (TextView) findViewById(R.id.textView1);
  40. bt1.setOnClickListener(this);
  41. bt2.setOnClickListener(this);
  42. bt3.setOnClickListener(this);
  43. client = new DefaultHttpClient();// 创建HttpClient对象
  44. }
  45. @Override
  46. public void onClick(View v) {
  47. switch (v.getId()) {
  48. case R.id.button1:// HttpClientGet请求网络
  49. new AsyncTask<String, Void, String>() {
  50. @Override
  51. protected String doInBackground(String... arg0) {
  52. String urlString = arg0[0];
  53. HttpGet get = new HttpGet(urlString);// 创建请求方法的实例
  54. try {
  55. HttpResponse response = client.execute(get);//获取连接响应信息
  56. // 可以调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头
  57. HttpEntity httpEntity = response.getEntity();// 获取HttpEntity实体
  58. // HttpEntity可以是流也可以是字符串,封装了一些对字符和流的操作,还有另外几种Entity实体类
  59. // String value = EntityUtils.toString( httpEntity
  60. // );//不加utf-8出现中文乱码
  61. String value = EntityUtils.toString(httpEntity, "utf-8");
  62. return value;
  63. } catch (ClientProtocolException e) {
  64. e.printStackTrace();
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. return null;
  69. }
  70. @Override
  71. protected void onPostExecute(String result) {
  72. tv.setText(result);
  73. }
  74. }.execute("http://www.whu.edu.cn/index.htm");
  75. break;
  76. case R.id.button2:// HttpClientPost请求网络
  77. new AsyncTask<String, Void, String>() {
  78. @Override
  79. protected String doInBackground(String... arg0) {
  80. HttpPost post = new HttpPost(arg0[0]);// 创建请求方法的实例
  81. try {
  82. List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
  83. list.add(new BasicNameValuePair("name", "whu"));
  84. post.setEntity(new UrlEncodedFormEntity(list));// 设置请求参数
  85. } catch (UnsupportedEncodingException e1) {
  86. }
  87. try {
  88. HttpResponse response = client.execute(post);// 发送请求获取相应
  89. HttpEntity httpEntity = response.getEntity();// 获取HttpEntity实体
  90. // HttpEntity可以是流也可以是字符串,封装了一些对字符和流的操作,还有另外几种Entity实体类
  91. // String value = EntityUtils.toString( httpEntity
  92. // );//不加utf-8出现中文乱码
  93. String value = EntityUtils.toString(httpEntity, "utf-8");
  94. return value;
  95. } catch (ClientProtocolException e) {
  96. e.printStackTrace();
  97. } catch (IOException e) {
  98. e.printStackTrace();
  99. }
  100. return null;
  101. }
  102. @Override
  103. protected void onPostExecute(String result) {
  104. tv.setText(result);
  105. }
  106. }.execute("http://www.whu.edu.cn/index.htm");
  107. break;
  108. case R.id.button3:// 获取图片
  109. new AsyncTask<String, Void, Bitmap>() {
  110. @Override
  111. protected Bitmap doInBackground(String... params) {
  112. try {
  113. URL url = new URL(params[0]);
  114. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  115. conn.setRequestMethod("GET");
  116. //setConnectTimeout:设置连接主机超时(单位:毫秒)
  117. //setReadTimeout:设置从主机读取数据超时(单位:毫秒)
  118. conn.setReadTimeout(5 * 1000);//设置从主机读取超时时间为5秒
  119. int code = conn.getResponseCode();//获取请求码,不设置setReadTimeout可能阻塞线程
  120. if (code == 200) {//正常返回
  121. InputStream inputStream = conn.getInputStream();//获取字节流
  122. Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
  123. return bitmap;
  124. }
  125. } catch (Exception e) {
  126. e.printStackTrace();
  127. }
  128. return null;
  129. }
  130. @Override
  131. protected void onPostExecute(Bitmap result) {
  132. if (result != null) {
  133. tv.setBackground(new BitmapDrawable(result));
  134. }
  135. }
  136. }.execute("http://www.whu.edu.cn/images/2017021002.jpg");
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. }

0 0
原创粉丝点击