zoukankan      html  css  js  c++  java
  • ANDROID_MARS学习笔记_S04_003_用HttpClent发http请求

    一、代码

    1.xml
    (1)activity_main.xml

     1     <TextView
     2         android:layout_width="wrap_content"
     3         android:layout_height="wrap_content"
     4         android:text="发送Http请求"
     5         android:onClick="sendHttpRequest" />
     6     <Button
     7         android:layout_width="wrap_content"
     8         android:layout_height="wrap_content"
     9         android:text="发送Http请求"
    10         android:onClick="sendHttpRequest" />
    11 
    12 </LinearLayout>

    (2)AndroidManifest.xml

    增加

    1 <uses-permission android:name="android.permission.INTERNET"/>

    2.java
    (1)MainActivity.java

     1 package com.http;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStream;
     6 import java.io.InputStreamReader;
     7 
     8 import org.apache.http.HttpEntity;
     9 import org.apache.http.HttpResponse;
    10 import org.apache.http.client.ClientProtocolException;
    11 import org.apache.http.client.HttpClient;
    12 import org.apache.http.client.methods.HttpGet;
    13 import org.apache.http.impl.client.DefaultHttpClient;
    14 
    15 import android.annotation.SuppressLint;
    16 import android.app.Activity;
    17 import android.os.Bundle;
    18 import android.os.StrictMode;
    19 import android.view.View;
    20 
    21 @SuppressLint("NewApi")
    22 public class MainActivity extends Activity {
    23 
    24     @Override
    25     protected void onCreate(Bundle savedInstanceState) {
    26         super.onCreate(savedInstanceState);
    27         setContentView(R.layout.activity_main);
    28         if (android.os.Build.VERSION.SDK_INT > 9) {
    29             StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    30             StrictMode.setThreadPolicy(policy);
    31         }
    32     }
    33     
    34     public void sendHttpRequest(View view) {
    35         System.out.println("sendHttpRequest---->");
    36         //生成一个请求对象
    37         HttpGet httpGet = new HttpGet("http://www.baidu.com");
    38         //生成一个Http客户端对象
    39         HttpClient httpClient = new DefaultHttpClient();
    40         InputStream inputStream = null;
    41         //使用Http客户端发送请求对象
    42         try {
    43             HttpResponse httpResponse = httpClient.execute(httpGet);
    44             HttpEntity httpEntity = httpResponse.getEntity();
    45             inputStream = httpEntity.getContent();
    46             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    47             String line = null;
    48             StringBuilder sb = new StringBuilder();
    49             while ((line = reader.readLine()) != null) {
    50                 sb.append(line);
    51             }
    52             System.out.println(sb);
    53         } catch (ClientProtocolException e) {
    54             e.printStackTrace();
    55         } catch (IOException e) {
    56             e.printStackTrace();
    57         } finally {
    58             try {
    59                 inputStream.close();
    60             } catch (IOException e) {
    61                 e.printStackTrace();
    62             }
    63         }
    64         
    65     }
    66 }

     

     

  • 相关阅读:
    vue 中引用echarts 初始化init undefind问题(Cannot read property ‘init‘ of undefined)
    粘性定位(position:sticky)实现固定表格表头、固定列
    js替换字符串中的空格,换行符 或 替换成<br>
    一个完整的大作业
    数据结构化与保存
    爬取所有校园新闻
    用requests库和BeautifulSoup4库爬取新闻列表
    中文词频统计及词云制作
    组合数据类型练习,英文词频统计实例
    字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理
  • 原文地址:https://www.cnblogs.com/shamgod/p/5204989.html
Copyright © 2011-2022 走看看