okhttp进行网络传输文件
其中使用了RXJava。
public class HttpDataManager {
private static HttpDataManager INSTANCE;
private RequestService service;
private static String ONLINE_URL = "http://baidu.com/";
public static HttpDataManager getInstance() {
if (INSTANCE == null) {
INSTANCE = new HttpDataManager();
}
return INSTANCE;
}
public HttpDataManager() {
this.service = createService();
}
private RequestService createService() {
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(ONLINE_URL)
.addConverterFactory(ResponseConvertFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
return retrofit.create(RequestService.class);
}
public RequestService getService() {
return service;
}
}
public interface RequestService {
/**
* getOcrRecog
* @return
*/
@Multipart
@POST("reventondc/v5/ocr/json")
Observable<OcrRecogResult> getOcrRecog(@Part MultipartBody.Part file);
}
public class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private final Gson gson;
private final Type type;
GsonResponseBodyConverter(Gson gson, Type type) {
this.gson = gson;
this.type = type;
}
@Override
public T convert(ResponseBody value) throws IOException {
String response = value.string();
checkError(response);
return gson.fromJson(response, type);
}
public void checkError(String response) throws MyHttpException {
try {
JSONObject rawResponeJson = new JSONObject(response);
int code = 200;
String msg = "";
if (rawResponeJson.has("code")) {
code = rawResponeJson.getInt("code");
}
if (rawResponeJson.has("message")) {
msg = rawResponeJson.getString("message");
}
if (code != 200) {
throw new MyHttpException(code, msg);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public class ResponseConvertFactory extends Converter.Factory {
/**
* Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and
* decoding from JSON (when no charset is specified by a header) will use UTF-8.
*/
public static ResponseConvertFactory create() {
return create(new Gson());
}
/**
* Create an instance using {@code gson} for conversion. Encoding to JSON and
* decoding from JSON (when no charset is specified by a header) will use UTF-8.
*/
public static ResponseConvertFactory create(Gson gson) {
return new ResponseConvertFactory(gson);
}
private final Gson gson;
private ResponseConvertFactory(Gson gson) {
if (gson == null) {
throw new NullPointerException("gson == null");
}
this.gson = gson;
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
return new GsonResponseBodyConverter<>(gson,type);
}
}
public class MyHttpException extends RuntimeException {
public int code;
public String msg;
public MyHttpException(int code, String msg){
super(msg);
this.code = code;
this.msg = msg;
}
}
调用方式:
mOcrConsumer = new Consumer<OcrRecogResult>() {
@Override
public void accept(OcrRecogResult ocrRecogResult) throws Exception {
if (mCallBack != null) {
mCallBack.success(ocrRecogResult);
}
}
};
mThrowableConsumer = new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
if (mCallBack != null) {
mCallBack.fail(throwable.getMessage());
}
}
};
传输本地图片文件:
File file = new File(filepath);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("pic", file.getName(), requestFile);
HttpDataManager.getInstance().getService().getOcrRecog(body).subscribeOn(Schedulers.io())
.subscribe(mOcrConsumer, mThrowableConsumer);
传输内存图片:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), byteArrayOutputStream.toByteArray());
MultipartBody.Part body = MultipartBody.Part.createFormData("pic", "src.jpg", requestFile); HttpDataManager.getInstance().getService().getOcrRecog(body).subscribeOn(Schedulers.io())
.subscribe(mOcrConsumer, mThrowableConsumer);
okhttp进行网络传输文件的更多相关文章
- Android网络编程只局域网传输文件
Android网络编程之局域网传输文件: 首先创建一个socket管理类,该类是传输文件的核心类,主要用来发送文件和接收文件 具体代码如下: package com.jiao.filesend; im ...
- 网络传输速度bps与下载文件所需时间的换算
相信很多同志都非常关注自己家的计算机上网的宽带是多少.关心单位上网的宽带是多少! 但是很多同志都经常误解网络传输速度,以至于责备网络接入商(电信.网通.铁通等单位)欺骗用户,限制上网的速度! 本文,就 ...
- java socket 多线程网络传输多个文件
http://blog.csdn.net/njchenyi/article/details/9072845 java socket 多线程网络传输多个文件 2013-06-10 21:26 3596人 ...
- c/c++ 网络编程 文件传输
网络编程 文件传输 1,文件发送端 2,文件接收端 文件发送端: #include <iostream> #include <string.h> #include <sy ...
- PHP-02.文件上传、php保存/转移上传的文件、常见的网络传输协议、请求报文及属性、响应报文及属性
关系数组 array("key"=>"value",...) ; get没有数据大小的限制 post上传大小没有限制 不指定上传方式,默认是get 文件上 ...
- App安全之网络传输安全
移动端App安全如果按CS结构来划分的话,主要涉及客户端本身数据安全,Client到Server网络传输的安全,客户端本身安全又包括代码安全和数据存储安全.所以当我们谈论App安全问题的时候一般来说在 ...
- APP安全--网络传输安全 AES/RSA/ECC/MD5/SHA
移动端App安全如果按CS结构来划分的话,主要涉及客户端本身数据安全,Client到Server网络传输的安全,客户端本身安全又包括代码安全和数据存储安全.所以当我们谈论App安全问题的时候一般来说在 ...
- mac与windows通过ftp传输文件
1.两个系统相互传文件,比较通用的方式是用QQ,两台电脑一台各登陆一个qq,发文件就行了,在同一个网段时,qq会自动转换为按局域网的方式传输. 2.本人不愿安装qq,以ftp方式进行传输,先在wndo ...
- Servlet学习三——传输文件
最先在考虑传输文件时,想通过java写一个文件上传案例,传给Servlet,Servlet再保存至数据库中,但苦于一直没找到实例,听说Flex有实际的例子,就直接用Flex例子来测试了.本文的顺序为: ...
随机推荐
- LINUX mysql 源码安装
一.下载编译安装 #cd /usr/local/src/ #wget http://mysql.byungsoo.net/Downloads/MySQL-5.1/mysql-5.1.38.tar.gz ...
- oracle-systemtap
https://github.com/hatem-mahmoud/scripts https://mahmoudhatem.wordpress.com/2015/05/26/stapora-v1-0- ...
- C#程序集系列11,全局程序集缓存
全局程序集缓存(GAC:Global Assembly Cache)用来存放可能被多次使用的强名称程序集.当主程序需要加载程序集的时候,优先选择到全局程序集缓存中去找寻需要的程序集. 为什么需要全局程 ...
- python接口自动化24-有token的接口项目使用unittest框架设计
获取token 在做接口自动化的时候,经常会遇到多个用例需要用同一个参数token,并且这些测试用例跨.py脚本了. 一般token只需要获取一次就行了,然后其它使用unittest框架的测试用例全部 ...
- Mantis的config_inc.php的各配置项的作用及其修改
Mantis的设置是这样保存的:在 config_defaults_inc.php中保存Mantis的默认设置,用户自己的设置信息保存在config_inc.php中.如果某个选项在config_in ...
- Guava Files 源码分析(一)
Files中的工厂 Files类中对InputStream, OutputStream以及Reader,Writer的操作封装了抽象工厂模式,抽象工厂是InputSupplier与OutputSupp ...
- poj 2284 That Nice Euler Circuit 解题报告
That Nice Euler Circuit Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 1975 Accepted ...
- 定义和使用EL函数
EL为表达式语言,在EL中,允许定义和使用函数.下面将介绍如何定义和使用EL的函数. 1. 定义和使用函数 函数的定义和使用分为以下3个步骤: (1)编写一个Java类,并在该类中编写公用的静态方法, ...
- [10] 圆管(Pipe)图形的生成算法
顶点数据的生成 bool YfBuildPipeVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices, YeO ...
- Informatica 常用组件Lookup缓存之五 使用动态查找高速缓存
对于关系查找,当目标表也是查找表时,可能要配置转换以使用动态高速缓存.PowerCenter 将在处理第一个查找请求时创建高速缓存.它将根据查找条件为传递给转换的每行查询高速缓存.当您使用动态高速缓存 ...