URLConnection和HttpClient使用入门
- <?xml version="1.0" encoding="utf-8"?>
- <linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"> - <textview android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/TextView01"
android:text="网络连接测试"> - <button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/Button01"
android:text="使用URLConnection访问GoogleWeatherAPI"> - </button>
- <button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/Button02"
android:text="使用HttpClient访问GoogleWeatherAPI"> - </button>
- <scrollview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ScrollView01">
- <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextView02">
- </textview>
- </scrollview>
- </textview></linearlayout>
复制代码
3、MainActivity.java的内容如下:
- package android.basic.lesson30;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import org.apache.http.client.ResponseHandler;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.BasicResponseHandler;
- import org.apache.http.impl.client.DefaultHttpClient;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- TextView tv;
- String googleWeatherUrl1 = "http://www.google.com/ig/api?weather=zhengzhou";
- String googleWeatherUrl2 = "http://www.google.com/ig/api?hl=zh-cn&weather=zhengzhou";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 定义UI组件
- Button b1 = (Button) findViewById(R.id.Button01);
- Button b2 = (Button) findViewById(R.id.Button02);
- tv = (TextView) findViewById(R.id.TextView02);
- // 设置按钮单击监听器
- b1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 使用URLConnection连接GoogleWeatherAPI
- urlConn();
- }
- });
- // 设置按钮单击监听器
- b2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 使用HttpCient连接GoogleWeatherAPI
- httpClientConn();
- }
- });
- }
- // 使用URLConnection连接GoogleWeatherAPI
- protected void urlConn() {
- try {
- // URL
- URL url = new URL(googleWeatherUrl1);
- // HttpURLConnection
- HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
- if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
- Toast.makeText(getApplicationContext(), "连接Google Weather API成功!",
- Toast.LENGTH_SHORT).show();
- // InputStreamReader
- InputStreamReader isr = new InputStreamReader(httpconn.getInputStream(), "utf-8");
- int i;
- String content = "";
- // read
- while ((i = isr.read()) != -1) {
- content = content + (char) i;
- }
- isr.close();
- //设置TextView
- tv.setText(content);
- }
- //disconnect
- httpconn.disconnect();
- } catch (Exception e) {
- Toast.makeText(getApplicationContext(), "连接Google Weather API失败", Toast.LENGTH_SHORT)
- .show();
- e.printStackTrace();
- }
- }
- // 使用HttpCient连接GoogleWeatherAPI
- protected void httpClientConn() {
- //DefaultHttpClient
- DefaultHttpClient httpclient = new DefaultHttpClient();
- //HttpGet
- HttpGet httpget = new HttpGet(googleWeatherUrl2);
- //ResponseHandler
- ResponseHandler<string> responseHandler = new BasicResponseHandler();
- try {
- String content = httpclient.execute(httpget, responseHandler);
- Toast.makeText(getApplicationContext(), "连接Google Weather API成功!",
- Toast.LENGTH_SHORT).show();
- //设置TextView
- tv.setText(content);
- } catch (Exception e) {
- Toast.makeText(getApplicationContext(), "连接Google Weather API失败", Toast.LENGTH_SHORT)
- .show();
- e.printStackTrace();
- }
- httpclient.getConnectionManager().shutdown();
- }
- }</string>
复制代码
4、
最后别忘了在AndroidManifest.xml中加入访问网络的权限,<uses-permission
android:name="android.permission.INTERNET"></uses-permission>5、
运行程序查看结果:

按第一个按钮的效果,返回的数据结果显示在了TextView里。

按第二个按钮的效果,返回的数据结果显示在了TextView里, 所不同的是显示的是中文。好了,本讲先到这里。
URLConnection和HttpClient使用入门的更多相关文章
- java学习-GET方式抓取网页(UrlConnection和HttpClient)
抓取网页其实就是模拟客户端(PC端,手机端...)发送请求,获得响应数据documentation,解析对应数据的过程.---自己理解,错误请告知 一般常用请求方式有GET,POST,HEAD三种 G ...
- 《Android学习指南》目录
源:<Android学习指南>目录 Android学习指南的内容分类: 分类 描述 0.学习Android必备的Java基础知识 没有Java基础的朋友,请不要先看Android的课程,这 ...
- 《Android学习指南》文件夹
转自:http://android.yaohuiji.com/about Android学习指南的内容分类: 分类 描写叙述 0.学习Android必备的Java基础知识 没有Java基础的朋友,请不 ...
- 基于开源 Openfire 聊天服务器 - 开发Openfire聊天记录插件[转]
上一篇文章介绍到怎么在自己的Java环境中搭建openfire插件开发的环境,同时介绍到怎样一步步简单的开发openfire插件.一步步很详细的介绍到简单插件开发,带Servlet的插件的开发.带JS ...
- openfire:基于开源 Openfire 聊天服务器 - 开发Openfire聊天记录插件
基于开源 Openfire 聊天服务器 - 开发Openfire聊天记录插件 上一篇文章介绍到怎么在自己的Java环境中搭建openfire插件开发的环境,同时介绍到怎样一步步简单的开发openfir ...
- 基于开源 Openfire 聊天服务器 - 开发Openfire聊天记录插件
原文:http://www.cnblogs.com/hoojo/archive/2013/03/29/openfire_plugin_chatlogs_plugin_.html 随笔-150 评论- ...
- 我的简历 PHP Java C# 技术总监
石先生 ID:303321266 目前正在找工作 13611326258 hr_msn@163.com 男|32 岁 (1985/08/06)|现居住北京-海淀区|12年工作经验 ...
- java实现 HTTP/HTTPS请求绕过证书检测代码实现
java实现 HTTP/HTTPS请求绕过证书检测代码实现 1.开发需求 需要实现在服务端发起HTTP/HTTPS请求,访问其他程序资源. 2.URLConnection和HTTPClient的比较 ...
- asp.net hessian + android hessdroid
做android开发时你还在为gson,json而人肉序列化与反序列化吗,上传文件时你还在使用UrlConnection或者HttpClient吗?下面提供了asp.net 服务端与 android ...
随机推荐
- 让声音更清晰,用PR去掉视频中的噪音
Premiere V7.0没有支持当前文件类型的输入源的问题 25 我是新手,把DVD影片放到电脑里,显示的是VOB文件,导入类型显示的是所有支持格式,于是我就点击了VOB文件,但是说没有支持当前文件 ...
- UVM中的regmodel建模(三)
总结一下UVM中的寄存器访问实现: 后门访问通过add_hdl_path命令来添加寄存器路径,并扩展uvm_reg_backdoor基类,定义read与write函数,最后在uvm_reg_block ...
- Intro to Python for Data Science Learning 8 - NumPy: Basic Statistics
NumPy: Basic Statistics from:https://campus.datacamp.com/courses/intro-to-python-for-data-science/ch ...
- 持续集成之三:Linux安装Jenkins
环境 Red Hat Enterprise Linux Server release 7.3 (Maipo) jdk1.7.0_80 apache-tomcat-7.0.90 jenkin ...
- linux常用命令:Linux 目录结构
对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...
- mysql创建外链失败1005错误解决方法
mysql创建外链失败1005错误解决方法 错误号:1005错误信息:Can't create table 'webDB.#sql-397_61df' (errno: 150)解决方法 错误原因有四: ...
- web测试项目总结
一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&符号.禁止直接输入特殊字符时,使 ...
- mysql的数据类型和字符集
MySQL的数据类型 MySQL数据库支持的数据类型主要有以下几种: 整型 浮点型 字符 BLOB型 枚举和集合类型 JSON类型(MySQL5.7新增加的支持) 整型 整数类型是数据库中最基本的数据 ...
- Shell学习笔记之shell脚本和python脚本实现批量ping IP测试
0x00 将IP列表放到txt文件内 先建一个存放ip列表的txt文件: [root@yysslopenvpn01 ~]# cat hostip.txt 192.168.130.1 192.168.1 ...
- Python入门之logging模块
本章目录: 一.logging模块简介 二.logging模块的使用 三.通过JSON或者YMAL文件配置logging模块 ===================================== ...