1.

首先是进行网络访问权限设置

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.exp.httpdemo"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="8"
  9. android:targetSdkVersion="18" />
  10.  
  11. <application
  12. android:allowBackup="true"
  13. android:icon="@drawable/ic_launcher"
  14. android:label="@string/app_name"
  15. android:theme="@style/AppTheme" >
  16. <activity
  17. android:name="com.exp.httpdemo.MainActivity"
  18. android:label="@string/app_name" >
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21.  
  22. <category android:name="android.intent.category.LAUNCHER" />
  23. </intent-filter>
  24. </activity>
  25. </application>
  26.  
  27. </manifest>

 

  1. 1 package com.exp.httpdemo;
  2. 2
  3. 3 import java.io.InputStream;
  4. 4
  5. 5 import org.apache.http.HttpEntity;
  6. 6 import org.apache.http.HttpResponse;
  7. 7 import org.apache.http.client.HttpClient;
  8. 8 import org.apache.http.client.methods.HttpGet;
  9. 9 import org.apache.http.impl.client.DefaultHttpClient;
  10. 10 import org.apache.http.util.EntityUtils;
  11. 11
  12. 12 import android.os.Bundle;
  13. 13 import android.os.Handler;
  14. 14 import android.app.Activity;
  15. 15 import android.view.Menu;
  16. 16 import android.view.View;
  17. 17 import android.widget.TextView;
  18. 18
  19. 19 /**
  20. 20 * 使用HttpClient请求一个网页
  21. 21 *
  22. 22 */
  23. 23 public class MainActivity extends Activity {
  24. 24 TextView tvResult;
  25. 25 String url="http://www.baidu.com";
  26. 26 Handler handler = new Handler();
  27. 27 @Override
  28. 28 protected void onCreate(Bundle savedInstanceState) {
  29. 29 super.onCreate(savedInstanceState);
  30. 30 setContentView(R.layout.activity_main);
  31. 31
  32. 32 tvResult=(TextView)findViewById(R.id.textView1);
  33. 33 }
  34. 34
  35. 35 public void ClickButton(View view){
  36. 36 new Thread(new Runnable() {
  37. 37
  38. 38 @Override
  39. 39 public void run() {
  40. 40 // 获取HttpClient对象
  41. 41 HttpClient httpClient = new DefaultHttpClient();
  42. 42 // 获取HttpGet对象
  43. 43 HttpGet requestGet = new HttpGet(url);
  44. 44 try {
  45. 45 // 获取响应结果
  46. 46 HttpResponse httpResponse = httpClient.execute(requestGet);
  47. 47 if (httpResponse.getStatusLine().getStatusCode() == 200) {
  48. 48 // 从响应中取出实体类
  49. 49 HttpEntity httpEntity = httpResponse.getEntity();
  50. 50 final String result=EntityUtils.toString(httpEntity);
  51. 51 // 如果想通过IO流来读写可以使用getContent方法获取
  52. 52 //InputStream in = httpEntity.getContent();
  53. 53 handler.post(new Runnable() {
  54. 54
  55. 55 @Override
  56. 56 public void run() {
  57. 57 tvResult.setText(result);
  58. 58 }
  59. 59 });
  60. 60 }
  61. 61 } catch (Exception e) {
  62. 62 e.printStackTrace();
  63. 63
  64. 64 }
  65. 65 }
  66. 66 }).start();
  67. 67
  68. 68
  69. 69 }
  70. 70 @Override
  71. 71 public boolean onCreateOptionsMenu(Menu menu) {
  72. 72 // Inflate the menu; this adds items to the action bar if it is present.
  73. 73 getMenuInflater().inflate(R.menu.main, menu);
  74. 74 return true;
  75. 75 }
  76. 76
  77. 77 }

 

版权声明:本文为jingtonglong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/jingtonglong/articles/4891171.html