其中使用了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进行网络传输文件的更多相关文章

  1. Android网络编程只局域网传输文件

    Android网络编程之局域网传输文件: 首先创建一个socket管理类,该类是传输文件的核心类,主要用来发送文件和接收文件 具体代码如下: package com.jiao.filesend; im ...

  2. 网络传输速度bps与下载文件所需时间的换算

    相信很多同志都非常关注自己家的计算机上网的宽带是多少.关心单位上网的宽带是多少! 但是很多同志都经常误解网络传输速度,以至于责备网络接入商(电信.网通.铁通等单位)欺骗用户,限制上网的速度! 本文,就 ...

  3. java socket 多线程网络传输多个文件

    http://blog.csdn.net/njchenyi/article/details/9072845 java socket 多线程网络传输多个文件 2013-06-10 21:26 3596人 ...

  4. c/c++ 网络编程 文件传输

    网络编程 文件传输 1,文件发送端 2,文件接收端 文件发送端: #include <iostream> #include <string.h> #include <sy ...

  5. PHP-02.文件上传、php保存/转移上传的文件、常见的网络传输协议、请求报文及属性、响应报文及属性

    关系数组 array("key"=>"value",...) ; get没有数据大小的限制 post上传大小没有限制 不指定上传方式,默认是get 文件上 ...

  6. App安全之网络传输安全

    移动端App安全如果按CS结构来划分的话,主要涉及客户端本身数据安全,Client到Server网络传输的安全,客户端本身安全又包括代码安全和数据存储安全.所以当我们谈论App安全问题的时候一般来说在 ...

  7. APP安全--网络传输安全 AES/RSA/ECC/MD5/SHA

    移动端App安全如果按CS结构来划分的话,主要涉及客户端本身数据安全,Client到Server网络传输的安全,客户端本身安全又包括代码安全和数据存储安全.所以当我们谈论App安全问题的时候一般来说在 ...

  8. mac与windows通过ftp传输文件

    1.两个系统相互传文件,比较通用的方式是用QQ,两台电脑一台各登陆一个qq,发文件就行了,在同一个网段时,qq会自动转换为按局域网的方式传输. 2.本人不愿安装qq,以ftp方式进行传输,先在wndo ...

  9. Servlet学习三——传输文件

    最先在考虑传输文件时,想通过java写一个文件上传案例,传给Servlet,Servlet再保存至数据库中,但苦于一直没找到实例,听说Flex有实际的例子,就直接用Flex例子来测试了.本文的顺序为: ...

随机推荐

  1. docker 图解学习

    http://www.cnblogs.com/bethal/tag/docker/  

  2. QT 开发资料

    http://pan.baidu.com/s/1bntYkpx 密码: fgsa

  3. 【从零学习openCV】IOS7人脸识别实战

    前言 接着上篇<IOS7下的人脸检測>,我们顺藤摸瓜的学习怎样在IOS7下用openCV的进行人脸识别,实际上非常easy,因为人脸检測部分已经完毕,剩下的无非调用openCV的方法对採集 ...

  4. Starting nginx: nginx: [emerg] bind() to 0.0.0.0:8088 failed (13: Permission denied) nginx 启动失败

     Starting nginx: nginx: [emerg] bind() to 0.0.0.0:8088 failed (13: Permission denied)     nginx 启动失败 ...

  5. 【spring cloud】【IDEA】【maven】spring cloud多模块在idea上使用maven插件打包报错:程序包XXX不存在

    >>>>spring cloud 多模块 >>>>在idea上使用maven插件打包,欲打包成jar包后 进行部署 >>>> 报 ...

  6. Ladda 应用提交表单的时候显示loading载入中 包含不同位置,不同效果

    Ladda 应用提交表单的时候显示loading载入中 包含不同位置,不同效果 不同大小.位置,效果,进度条等 演示 XML/HTML Code <article class="exa ...

  7. C++中new的用法及显示调用析构函数

    最近被问到了C++内存池的问题,其中不免涉及到在指定内存地址调用对象构造函数以及显示调用对象析构函数的情况. C++中new的用法 new是C++中用于动态内存分配的运算符,在C语言中一般使用mall ...

  8. longest-substring-with-at-least-k-repeating-characters

    https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/ public class S ...

  9. 什么是'脑分裂(split brain)'?

    这个词明显有点恐怖.设想一下,如果某时刻连接两个控制器之间的通路出现了问题,而不是其中某个控制器死机,此时两个控制器其实都是工作正常的,但是两者都检测不到对方的存在,所以两者都尝试接管所有总线,这时候 ...

  10. iOS开发-UI基础Demo

    现在更多的学习资料都是xCode4.X的,发现xCode6.1还是很多东西,如果有正在学习iOS开发的可以通过Demo简单了解下iOS的UI开发~ 1.新建单视图文件: 2.新建项目名称,语言选择OC ...