Android进阶2之Http操作访问网络

news/2024/7/3 4:17:43


操作步骤:

<1>

生成请求对象

HttpGet httpGet = new HttpGet("请求地址。。。。。");

<2>

生成客户端对象

HttpClient httpClient = new DefaultHttpClient();

<3>

执行请求

HttpResponse httpResponse = httpClient.execute(httpGet);

<4>

接受响应

HttpEntity  httpEntity = httpResponse.getEntity();

<5>得到数据流

InputStream  inputStream = httpEntity.getContent();

注意:

要添加权限: <uses-permission android:name="android.permission.INTERNET" />

具体实现:

package xiaosi.httpResponse; 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.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class HttpResponseActivity extends Activity { private Button button = null; private TextView text = null; private HttpResponse httpResponse = null; private HttpEntity httpEntity = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text = (TextView) findViewById(R.id.text); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 生成一个请求对象,参数就是地址 HttpGet httpGet = new HttpGet("http://www.baidu.com"); // 生成Http客户端 HttpClient httpClient = new DefaultHttpClient(); InputStream inputStream = null; // 使用HTTP客户端发送请求对象 try { // 发送请求的响应 httpResponse = httpClient.execute(httpGet); // 代表接收的http消息,服务器返回的消息都在httpEntity httpEntity = httpResponse.getEntity(); if(httpResponse.getStatusLine().getStatusCode() == 200){ inputStream = httpEntity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String result = ""; String line = ""; while ((line = reader.readLine()) != null) { result = result + line; } text.setText(result); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }); } }

转载于:https://www.cnblogs.com/snake-hand/archive/2012/03/23/2454372.html


http://www.niftyadmin.cn/n/3053397.html

相关文章

Docker自动部署Apache Tomcat

本文是Docker的入门文章&#xff0c;推荐Java开发者阅读。文章详细介绍了如何用Docker来安装部署Tomcat。 介绍本文会讲述&#xff1a; 扩展Tomcat的官方Dockerfile构建新的镜像从修改过的新镜像启动容器在容器里部署RESTful的Web服务并测试Apache Tomcat使用docker search可以…

cocos2d-x游戏实例(19)-纵版射击游戏(6)

小满&#xff08;bill man&#xff09;个人原创&#xff0c;欢迎转载&#xff0c;转载请注明地址&#xff0c;小满&#xff08;bill man&#xff09;的专栏地址http://blog.csdn.net/bill_man 上一篇的实例中&#xff0c;纵版射击游戏中加入了敌人对子弹的碰撞和爆炸特效&#…

iptable语法详解

这篇文章摘自《linux firewall》 3th&#xff0c;Novell press。如有需要可发邮件至&#xff1a;shanhuiyanggmail.comiptables SyntaxAs presented earlier, iptables uses the concept of separate rule tables for different packet processing functionality. Nondefault t…

《剑指offer》:[39-1]判断是否为平衡二叉树

题目&#xff1a;输入一棵二叉树的结点&#xff0c;判断该树是不是平衡二叉树。如果某二叉树中任意结点的左右子树的深度相差不超过1&#xff0c;那么它就是一棵平衡二叉树。上图中的二叉树就是一棵平衡二叉树。分析&#xff1a;有了求二叉树深度的思路后&#xff0c;我们很快就…

shell脚本下载

经过漫长的尝试终于学会了四行代码 ##在linux下运行 filename"coco2017labels.zip" fileid"1cXZR_ckHki6nddOmcysCuuJFM--T-Q6L" curl -c ./cookie -s -L "https://drive.google.com/uc?exportdownload&id${fileid}" > /dev/null #http…

[wp7软件]wp7~~密码管理软件~~集合贴~~

1 小时前 上传下载附件 (2.52 KB)[wp7软件]密码钱包 1.0.0.0 [wp7软件]随机密码 1.0.0.0 [wp7软件]密码生成器 1.0.0.0 [wp7软件]密码保存 1.0.0.0 [wp7软件]密码生成 1.0.0.0 [wp7软件]加强密码 1.0.0.0 [wp7软件]密码生成器 1.0.0.0 [wp7软件]密码管理 1.0.0.0 [wp7软…

python 线程使用

################# 线程演示脚本 ####################### #codingutf-8import threadingfrom time import ctime,sleep def music(func): for i in range(2): print "I was listening to %s. %s" %(func,ctime()) sleep(1) def move(func): fo…

网站、数据库的衍变之路(六)

上文中简单讲到了集群的原理&#xff0c;由于缺乏研究的环境&#xff0c;没办法自己组网实验&#xff0c;因此&#xff0c;贫道对这方面并没有深刻的理解&#xff0c;而只是停留在理论理解上。在评论里&#xff0c;看到有朋友让我讲讲这方面&#xff0c;那我就讲一些我的理解&a…