Android 最早使用的简单的网络请求
下面是最早从事android开发的时候写的网络请求的代码,简单高效,对于理解http请求有帮助。直接上代码,不用解释,因为非常简单。
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map; import org.apache.http.entity.mime.content.FileBody; import android.util.Log; public class HttpRequest { public static final String UTF_8 = "UTF-8"; private static String cookie = null; /**
* GET请求
*
* @param actionUrl
* @param params
* @return
*/
public static String httpGet(String actionUrl, Map<String, String> params) {
try{
StringBuffer urlbuff = new StringBuffer(actionUrl);
if (params != null && params.size() > 0) {
if (actionUrl.indexOf("?") >= 0) {
urlbuff.append("&");
} else {
urlbuff.append("?");
}
for (String key : params.keySet()) {
urlbuff.append(key).append("=").append(URLEncoder.encode(params.get(key), UTF_8)).append("&");
}
urlbuff.deleteCharAt(urlbuff.length() - 1);
Log.v("---request---Get---", urlbuff.toString());
}
URL url = new URL(urlbuff.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);// 允许输入
conn.setDoOutput(false);// 允许输出
conn.setUseCaches(false);// 不使用Cache
conn.setRequestMethod("GET");
conn.setRequestProperty("Charset", UTF_8);
if (cookie != null) {
conn.setRequestProperty("Cookie", cookie);
}
int cah = conn.getResponseCode();
if (cah != 200)
throw new RuntimeException("请求url失败");
if (conn.getHeaderField("Set-Cookie") != null) {
cookie = conn.getHeaderField("Set-Cookie");
}
Log.i("", "------------------cookie:" + cookie);
Map<String, List<String>> keys = conn.getHeaderFields();
for(String key : keys.keySet()) {
List<String> list = keys.get(key);
for(String value : list) {
Log.i("", "header: key:" + key + " values:" + value);
}
} InputStream is = conn.getInputStream();
int ch;
StringBuilder b = new StringBuilder();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
is.close();
conn.disconnect();
return b.toString();
}catch(Exception e) {
e.printStackTrace();
}
return null;
} /**
* post 带文件上传
* @param actionUrl
* @param params
* @param files
* @return
*/
public static String httpPost(String actionUrl, Map<String, String> params, Map<String, FileBody> files) {
String LINE_START = "--";
String LINE_END = "\r\n";
String BOUNDRY = "*****"; try{
HttpURLConnection conn = null;
DataOutputStream dos = null; int bytesRead, bytesAvailable, bufferSize;
long totalBytes;
byte[] buffer;
int maxBufferSize = 8096; URL url = new URL(actionUrl);
conn = (HttpURLConnection) url.openConnection(); // Allow Inputs
conn.setDoInput(true); // Allow Outputs
conn.setDoOutput(true); // Don't use a cached copy.
conn.setUseCaches(false); // Use a post method.
conn.setRequestMethod("POST");
//if (files != null && files.size() > 0) {
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+BOUNDRY);
//} Log.i("", "cookie:" + cookie);
if (cookie != null) {
conn.setRequestProperty("Cookie", cookie);
}
// // Set the cookies on the response
// String cookie = CookieManager.getInstance().getCookie(server);
// if (cookie != null) {
// conn.setRequestProperty("Cookie", cookie);
// } // // Should set this up as an option
// if (chunkedMode) {
// conn.setChunkedStreamingMode(maxBufferSize);
// } dos = new DataOutputStream(conn.getOutputStream()); // Send any extra parameters
for (Object key : params.keySet()) {
dos.writeBytes(LINE_START + BOUNDRY + LINE_END);
dos.writeBytes("Content-Disposition: form-data; name=\"" + key.toString() + "\"" + LINE_END);
dos.writeBytes(LINE_END);
dos.write(params.get(key).getBytes());
dos.writeBytes(LINE_END); Log.i("", "-----key:" + key + " value:" + params.get(key));
}
//-----------
if (files != null && files.size() > 0) {
for (String key : files.keySet()) {
Log.i("", "-----key:" + key + " value:" + params.get(key));
FileBody fileBody = files.get(key);
dos.writeBytes(LINE_START + BOUNDRY + LINE_END);
dos.writeBytes("Content-Disposition: form-data; name=\"" + key + "\";" + " filename=\"" + fileBody.getFilename() +"\"" + LINE_END);
dos.writeBytes("Content-Type: " + fileBody.getMimeType() + LINE_END);
dos.writeBytes(LINE_END); // Get a input stream of the file on the phone
InputStream fileInputStream = new FileInputStream(fileBody.getFile());
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
totalBytes = 0;
while (bytesRead > 0) {
totalBytes += bytesRead;
//result.setBytesSent(totalBytes);
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(LINE_END);
// close streams
fileInputStream.close();
}
}
dos.writeBytes(LINE_START + BOUNDRY + LINE_START + LINE_END);
dos.flush();
dos.close(); int statusCode = conn.getResponseCode();
Log.i("", "---------------statusCode:" + statusCode);
if (statusCode != 200) {
throw new HttpRequestException("server error");
} //------------------ read the SERVER RESPONSE
InputStream is = conn.getInputStream();
int ch;
StringBuilder b = new StringBuilder();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
conn.disconnect(); return b.toString();
}catch(Exception e){
Log.i("", "---------------" + e.getMessage(), e.fillInStackTrace());
e.printStackTrace();
}
return null;
} /**
* post请求
* @param actionUrl
* @param params
* @return
*/
public static String httpPost(String actionUrl, Map<String, String> params){
try{
URL url = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//因为这个是post请求,设立需要设置为true
conn.setDoOutput(true);
conn.setDoInput(true);
// 设置以POST方式
conn.setRequestMethod("POST");
// Post 请求不能使用缓存
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
if (cookie != null) {
conn.setRequestProperty("Cookie", cookie);
}
conn.connect();
//DataOutputStream流
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
//要上传的参数
StringBuffer content = new StringBuffer();
for (String key : params.keySet()) {
//String content = "par=" + URLEncoder.encode("ABCDEFG", "gb2312");
content.append(key).append("=").append(params.get(key)).append("&");
}
//将要上传的内容写入流中
out.writeBytes(content.toString());
//刷新、关闭
out.flush();
out.close(); int statusCode = conn.getResponseCode();
Log.i("", "---------------statusCode:" + statusCode);
if (statusCode != 200) {
throw new HttpRequestException("server error");
}
if (conn.getHeaderField("Set-Cookie") != null) {
cookie = conn.getHeaderField("Set-Cookie");
}
//获取数据
InputStream is = conn.getInputStream();
int ch;
StringBuilder b = new StringBuilder();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
conn.disconnect();
return b.toString();
}catch(Exception e) {
Log.i("", "---------------" + e.getMessage(), e.fillInStackTrace());
e.printStackTrace();
}
return null;
} }
1. application/x-www-form-urlencoded
最常见的 POST
提交数据的方式了。浏览器的原生 form 表单,如果不设置 enctype
属性,那么最终就会以 application/x-www-form-urlencoded
方式提交数据。
传递的key/val会经过URL转码,所以如果传递的参数存在中文或者特殊字符需要注意。
//例子
//b=曹,a=1 POST HTTP/1.1(CRLF)
Host: www.example.com(CRLF)
Content-Type: application/x-www-form-urlencoded(CRLF)
Cache-Control: no-cache(CRLF)
(CRLF)
b=%E6%9B%B9&a=1(CRLF)
//这里b参数的值"曹"因为URL转码变成其他的字符串了
2. text/xml
//例子 POST http://www.example.com HTTP/1.1(CRLF)
Content-Type: text/xml(CRLF)
(CRLF)
<?xml version="1.0"?>
<resource>
<id>123</id>
<params>
<name>
<value>homeway</value>
</name>
<age>
<value>22</value>
</age>
</params>
</resource>
3.application/json
//例子
//传递json POST HTTP/1.1(CRLF)
Host: www.example.com(CRLF)
Content-Type: application/json(CRLF)
Cache-Control: no-cache(CRLF)
Content-Length: 24(CRLF)
(CRLF)
{
"a":1,
"b":"hello"
}
4. multipart/form-data
使用表单上传文件时,必须让 form
的 enctyped
等于这个值。
并且Http协议会使用boundary来分割上传的参数
//例子
//a="曹",file1是一个文件 POST HTTP/1.1(CRLF)
Host: www.example.com(CRLF)
//注意data;和boundary之间有一个空格,并且----WebKitFormBoundary7MA4YWxkTrZu0gW是可以自定义的
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW(CRLF)
Cache-Control: no-cache(CRLF)
Content-Length: 728
(CRLF)
//如果有Content-Length的话,则Content-Length指下面所有的字节总数,包括boundary
//这里用自定义的boundary来进行分割,注意会在头部加多"--"
------WebKitFormBoundary7MA4YWxkTrZu0gW(CRLF)
Content-Disposition: form-data; name="a"(CRLF)
(CRLF)
曹(CRLF)
------WebKitFormBoundary7MA4YWxkTrZu0gW(CRLF)
Content-Disposition: form-data; name="file1"; filename="1.jpg"
Content-Type: application/octet-stream(CRLF)
(CRLF)
//此处是参数file1 对应的文件的二进制数据
[654dfasalk;af&6…](CRLF)
//最后一个boundary会分别在头部和尾部加多"--"
------WebKitFormBoundary7MA4YWxkTrZu0gW--(CRLF)
//多个文件同时上传
POST HTTP/1.1(CRLF)
Host: www.example.com(CRLF)
//注意data;和boundary之间有一个空格,并且----WebKitFormBoundary7MA4YWxkTrZu0gW是可以自定义的
Content-Type: multipart/form-data; boundary=---------------------------418888951815204591197893077
Cache-Control: no-cache(CRLF)
Content-Length: 12138(CRLF)
(CRLF)
-----------------------------418888951815204591197893077(CRLF)
// 文件1的头部boundary
Content-Disposition: form-data; name="userfile[]"; filename="文件1.md"(CRLF)
Content-Type: text/markdown(CRLF)
(CRLF)
// 文件1内容开始
// ...
// 文件1内容结束
-----------------------------418888951815204591197893077(CRLF)
// 文件2的头部boundary
Content-Disposition: form-data; name="userfile[]"; filename="文件2"(CRLF)
Content-Type: application/octet-stream(CRLF)
(CRLF)
// 文件2内容开始
// ...
// 文件2内容结束
-----------------------------418888951815204591197893077(CRLF)
// 文件3的头部boundary
Content-Disposition: form-data; name="userfile[]"; filename="文件3"(CRLF)
Content-Type: application/octet-stream(CRLF)
(CRLF)
// 文件3内容开始
// ...
// 文件3内容结束
-----------------------------418888951815204591197893077(CRLF)
// 参数username的头部boundary
Content-Disposition: form-data; name="username"(CRLF)
(CRLF)
zhangsan
-----------------------------418888951815204591197893077(CRLF)
// 参数password的头部boundary
Content-Disposition: form-data; name="password"(CRLF)
(CRLF)
zhangxx
-----------------------------418888951815204591197893077--
// 尾部boundary,表示结束
注意 :(CRLF)
指\r\n
Android 最早使用的简单的网络请求的更多相关文章
- Xamarin.Android之封装个简单的网络请求类
一.前言 回忆到上篇 <Xamarin.Android再体验之简单的登录Demo> 做登录时,用的是GET的请求,还用的是同步, 于是现在将其简单的改写,做了个简单的封装,包含基于Http ...
- 学习RxJava+Retrofit+OkHttp+MVP的网络请求使用
公司的大佬用的是这一套,那我这个菜鸟肯定要学习使用了. 我在网上找了很多文章,写的都很详细,比如 https://www.jianshu.com/u/5fd2523645da https://www. ...
- android通过fiddler代理,抓取网络请求
安装fiddler过程省略 1, 2, 3, 4,手机需要跟电脑处于同一局域网,设置网络代理为电脑在局域网内的ip,端口为3步设置的port 5,电脑就可以通过fiddler监控手机的所有网络请求了( ...
- iOS之ASIHttp简单的网络请求实现
描述: ASIHttpRequest是应用第三方库的方法,利用代码快,减少代码量,提高效率 准备工作: 一.导入第三方库ASIHttpRequest 二.会报很多的错,原因有两个,一个是要导入Xcod ...
- Xamarin 简单的网络请求
//try //{ // var httpReq = (HttpWebRequest)HttpWebRequest.Create(new Uri(re ...
- Java笔记7:最简单的网络请求Demo
一.服务器端 1 新建一个工程,建立一个名为MyRequest的工程. 2 FileàProject StructureàModulesà点击最右侧的“+”àLibraryàJava 找到Tomcat ...
- Android之三种网络请求解析数据(最佳案例)
AsyncTask解析数据 AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用. AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法. ...
- 基于Retrofit+RxJava的Android分层网络请求框架
目前已经有不少Android客户端在使用Retrofit+RxJava实现网络请求了,相比于xUtils,Volley等网络访问框架,其具有网络访问效率高(基于OkHttp).内存占用少.代码量小以及 ...
- Android 网络请求详解
我们知道大多数的 Android 应用程序都是通过和服务器进行交互来获取数据的.如果使用 HTTP 协议来发送和接收网络数据,就免不了使用 HttpURLConnection 和 HttpClient ...
随机推荐
- CentOS 修改源为163和指定epel源和docker安装
首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...
- Java的native关键字以及JNI
http://blog.csdn.net/yangjiali014/article/details/1633017 这篇博文相当清楚了 包括native关键字的介绍,JNI的书写步骤,以及JNI的实现 ...
- **python中的类和他的成员
面向对象是一种编程方式,此编程方式的实现基于对 类 和 对象 的使用. Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的. 在这里,为文章中使用 ...
- java 控制台 输入字符串
import java.util.Scanner; //导入输入类 public static void main(String[] args) { //创建输入对象 Scanner s ...
- Spring缓存注解@CachePut , @CacheEvict,@CacheConfig使用
Cacheable CachePut CacheEvict CacheConfig 开启缓存注解 @Cacheable @Cacheable是用来声明方法是可缓存的.将结果存储到缓存中以便后续使用相同 ...
- Java核心知识点 --- 线程中如何创建锁和使用锁 Lock , 设计一个缓存系统
理论知识很枯燥,但这些都是基本功,学完可能会忘,但等用的时候,会发觉之前的学习是非常有意义的,学习线程就是这样子的. 1.如何创建锁? Lock lock = new ReentrantLock(); ...
- docker plugin test
docker build -t docker-volume-drbd . id=$(docker create docker-volume-drbd true) docker export $id - ...
- screen command of linux
[screen command of linux] 常用键: 补充: Ctrl-a S # split terminal horizon Ctrl-a TAB # switch to ano ...
- ORA-00604: 递归 SQL 级别 1 出现错误 ORA-01000: 超出打开游标的最大数
有程序没关闭游标, --打开了哪些游标 select * from v$open_cursor 在open cursor之后一定要注意要close cursor(在store procedure里更应 ...
- SpringBoot整合ActiveMQ快速入门
Spring Boot 具有如下特性: 为基于 Spring 的开发提供更快的入门体验 开箱即用,没有代码生成,也无需 XML 配置.同时也可以修改默认值来满足特定的需求. 提供了一些大型项目中常见的 ...