一、代码

1.xml
(1)activity_main.xml

     <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送Http请求"
android:onClick="sendHttpRequest" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送Http请求"
android:onClick="sendHttpRequest" /> </LinearLayout>

(2)AndroidManifest.xml

增加

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

2.java
(1)MainActivity.java

 package com.http;

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; 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 android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View; @SuppressLint("NewApi")
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
} public void sendHttpRequest(View view) {
System.out.println("sendHttpRequest---->");
//生成一个请求对象
HttpGet httpGet = new HttpGet("http://www.baidu.com");
//生成一个Http客户端对象
HttpClient httpClient = new DefaultHttpClient();
InputStream inputStream = null;
//使用Http客户端发送请求对象
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
System.out.println(sb);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
}

ANDROID_MARS学习笔记_S04_003_用HttpClent发http请求的更多相关文章

  1. ANDROID_MARS学习笔记_S04_004_用HTTPCLENT发带参数的get和post请求

    一.代码 1.xml(1)activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...

  2. ANDROID_MARS学习笔记_S04_005_用sing-post向腾讯微博发一条信息

    一.代码流程 1.组织好sign-post需要的token,secrect 2.组织好发微博需要的信息 3.用sign-post进行签名 4.把签名结果从header中拿出来,转成entity,用ht ...

  3. ANDROID_MARS学习笔记_S01原始版_001_Intent

    一.Intent简介 二.代码 1.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.co ...

  4. ANDROID_MARS学习笔记_S01_012_RatingBar

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  5. ANDROID_MARS学习笔记_S01_012_SeekBar

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  6. ANDROID_MARS学习笔记_S01_011ProgressBar

    文档是这样来设置样式 <ProgressBar android:layout_width="wrap_content" android:layout_height=" ...

  7. ANDROID_MARS学习笔记_S01_010日期时间控件

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  8. ANDROID_MARS学习笔记_S01_009Relative_LAYOUT例子

    1. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android ...

  9. ANDROID_MARS学习笔记_S01_008Linear_layout例子

    1.netstone_layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

随机推荐

  1. sql2012 新增 OFFSET/FETCH

    SQL Server 对行数的排序提供了 TOP 筛选.SQL Server 2012 新增了 OFFSET 筛选. 一.TOP 筛选 如果有 ORDER BY 子句,TOP 筛选将根据排序的结果返回 ...

  2. redux学习笔记

    中文api:http://cn.redux.js.org/docs/react-redux/troubleshooting.html 3.6 Reducer Store 收到 Action 以后,必须 ...

  3. CAS Server 单点登录开源项目

    https://www.apereo.org/cas/download This project contains code that can extent an existing ASP.NET w ...

  4. iOS开发——视频播放 待更新……

    本文主要实现调用系统自带的播放器,自带的播放器已经做好了屏幕的适配,集成播放,暂停,快进等功能.能够满足基本的视频播放功能及iOS的视频播放的开发. 最后将其简单的封装成一个iOS视频播放工具类. 一 ...

  5. O-C相关05:方法的封装.

    前言:在 OC 中进行封装, 就是实现设置实例变量和获取实例变量数据的方法, 常常称为 setter 方法和 getter 方法. 或称为 get set 读写器. 1,setter 方法 sette ...

  6. javascript变量,类型 第9节

    1.变量,数据类型 <html> <head> <title>变量</title> </head> <body> <h3& ...

  7. Speak softly love

    Speak softly love and hold me warm against your heart 柔声诉情,拥我在你暖暖的心上 I feel your words, the tender t ...

  8. 「Windows MFC 」「Edit Control」 控件

    「Windows MFC 」「Edit Control」 控件

  9. 安装会声会影x8后打不开的解决方法

    操作系统:Windows 7 症状:双击程序图标后一直停留在购买介绍页,主程序界面一直无法打开 解决方法:删除系统补丁KB3126587和KB3126593

  10. Sdut 2165 Crack Mathmen(数论)(山东省ACM第二届省赛E 题)

    Crack Mathmen TimeLimit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Since mathmen take security ...