在Android开发中,使用Http协议实现网络之间的通信是随处可见的,使用http方式主要采用2中请求方式即get和post两种方式。

一、使用get方式:

HttpGet httpGet = new HttpGet(url);
// 生成一个客户端对象
httpClient = new DefaultHttpClient(); try {
// 通过客户端对象httpClient的execute方法执行请求
httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String result = "";
String line = "";
while ((line = reader.readLine()) != null) {
result = result + line;
}
System.out.println(result); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

二、使用Post方式:

NameValuePair nameValuePair1 = new BasicNameValuePair("name", name);
NameValuePair nameValuePair2 = new BasicNameValuePair("age", age); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(nameValuePair1);
nameValuePairs.add(nameValuePair2);
try {
httpEntity = new UrlEncodedFormEntity(nameValuePairs);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(httpEntity);
httpClient = new DefaultHttpClient(); try {
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream)); String result = "";
String line = "";
while ((line = reader.readLine()) != null) {
result = result + line; }
System.out.println(result); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}

Android实现Http协议案例的更多相关文章

  1. Android通过http协议POST传输方式

    Android通过http协议POST传输方式如下: 方式一:HttpPost(import org.apache.http.client.methods.HttpPost) 代码如下: privat ...

  2. Android 模拟HTTP协议的编码问题 Android默认编码UTF-8

    Android通过GET和POST方法请求服务器和浏览器请求的过程是不一样的. 浏览器请求服务器的时候会先将中文进行UTF-8编码,然后再发送到服务器端. Android编程下我们需要通过URLEnc ...

  3. 第四次作业——关于石墨文档(Android)客户端的案例分析

    关于石墨文档(Android)客户端的案例分析 作业地址:[https://edu.cnblogs.com/campus/nenu/2016CS/homework/2505] 第一部分调研,评测 1. ...

  4. Android开发---网格布局案例

     Android开发---网格布局案例 效果图: 1.MainActivity.java package com.example.android_activity; import android.ap ...

  5. Linux与Android 多点触摸协议【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/7833277 一.Linux与Android 多点触摸协议 为了使用功能强大的多点触控设 ...

  6. Linux & Android 多点触摸协议

    Linux & Android 多点触摸协议 Android4.0多点触摸入门 1 KERNEL 对于触摸屏的驱动我们简单的划分为两个主要的部分,一个是注册,另一个是上报. 1.1 注册 单点 ...

  7. 我的Android进阶之旅------>Android基于HTTP协议的多线程断点下载器的实现

    一.首先写这篇文章之前,要了解实现该Android多线程断点下载器的几个知识点 1.多线程下载的原理,如下图所示 注意:由于Android移动设备和PC机的处理器还是不能相比,所以开辟的子线程建议不要 ...

  8. android shareSDK 微博分享案例

    android shareSDK 微博分享案例 ShareSDK APP_KEY 219b1121fc68 腾讯微博 key 801517904 secret bfba83ae253c8f38dabe ...

  9. Android 使用 HTTP 协议访问网络

    正在看<第一行代码>,记录一下使用 HTTP 协议访问网络的内容吧! 在Android发送Http请求有两种方式,HttpURLConnection和HttpClient. 1.使用Htt ...

随机推荐

  1. Swift2.0 中的String(三):类型转换

    本系列第三篇,String相关的类型转换.其他的几篇传送门(GitHub打不开链接的同学请自行把地址github改成gitcafe,或者直接去归档里找:-P): Swift2.0 中的String(一 ...

  2. 云服务器 ECS Linux 系统 CPU 占用率较高问题排查思路

    https://help.aliyun.com/knowledge_detail/41225.html?spm=5176.7841174.2.2.ifP9Sc 注意:本文相关配置及说明已在 CentO ...

  3. linux文件操作命令--转

    引用地址:http://bbsunchen.iteye.com/blog/1010993 想自己一开始处理文件的时候,看文件有多少行,都要写个perl脚本,好不麻烦啊...以此文纪念自己傻逼的科研生活 ...

  4. ClamAV

    ClamAV 简介以及适用范围 ClamAV是一个在命令行下查毒软件,因为它不将杀毒作为主要功能,默认只能查出您计算机内的病毒,但是无法清除,至多删除文件.ClamAV可以工作很多的平台上,但是有少数 ...

  5. [golang学习] goroutine调度

    这两天有些闲功夫, 学习下golang, 确实非常简洁. 不过有些缺憾. 在我的测试中. golang的调度(goroutine)似乎不是非常好. func say(k int) { fmt.Prin ...

  6. LeetCode26 Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  7. WPF 之 线程使用

    但凡涉及到图形界面,往往的设计都是不支持或者不推荐使用多个线程操作界面内容.而且通常会有一个专门的线程调度器来处理任务线程和界面线程的问题. 下面提供两个方案: 1.使用Dispatcher.Begi ...

  8. 让ConfigurationManager打开任意的配置文件

    VisualStudio的配置文件很好很强大,用来保存数据库连接字符串或键值对都非常方便,只需要通过ConfigurationManager的ConnectionStrings或AppSettings ...

  9. Python笔记(一)

    我是一名211高校软件工程大三学生,由于前段时间一直在找实习公司.笔试面试了很多公司,虽然有一定的基础,但是还是被某些公司面试官像虐狗一样的虐了.最后找到了一个口碑比较好的外企,主攻信息安全方面.这段 ...

  10. 【Shell脚本学习13】Shell数组:shell数组的定义、数组长度

    Shell在编程方面比Windows批处理强大很多,无论是在循环.运算. bash支持一维数组(不支持多维数组),并且没有限定数组的大小.类似与C语言,数组元素的下标由0开始编号.获取数组中的元素要利 ...