在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. ccConfig(设置一些底层接口状态:是否支持动作叠加 设置fps更新间隔和位置 是否画边框等。。)

    #ifndef __CCCONFIG_H__ #define __CCCONFIG_H__ #include "platform/CCPlatformConfig.h" /** @ ...

  2. iOS开发——UI篇Swift篇&UISegmentedControl

    UISegmentedControl override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString / ...

  3. Android Widget 小部件(三) 在Activity中加入Widget

    package com.stone.ui; import static android.util.Log.d; import android.app.Activity; import android. ...

  4. linux 内核参数图解

    https://www.suse.com/documentation/sles11/book_sle_tuning/data/part_tuning_kernel.html http://blog.c ...

  5. SimpleDateFormat使用详解及与毫秒的相互转换

    1. SimpleDateFormat使用详解 public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方 ...

  6. global $GLOBALS区别

    <?phpfunction &test(){     static $b=0;//申明一个静态变量     $b=$b+1;     echo $b;     return $b; }} ...

  7. 慎用preg_replace危险的/e修饰符(一句话后门常用)

    要确保 replacement 构成一个合法的 PHP 代码字符串,否则 PHP 会在报告在包含 preg_replace() 的行中出现语法解析错误     preg_replace函数原型: mi ...

  8. 关于解决 Failed to prepare partial IU:

    在新版本的Eclipse(Luna)中安装插件经常会碰到Failed to prepare partial IU的错误,一把都是兼容性的问题,要下载个兼容包,步骤如下: 1.打开安装插件的页面:Hel ...

  9. LeetCode42 Trapping Rain Water

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  10. WPF 之 布局(二)

    一.Canvas  Canvas是最基本的面板,只是一个存储控件的容器,它不会自动调整内部元素的排列及大小,它仅支持用显式坐标定位控件,它也允许指定相对任何角的坐标,而不仅仅是左上角.可以使用Left ...