其中使用了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. FT232H USB转串口,I2C,JTAG高速芯片

    随着FT232H USB2.0高速芯片的发布,英商飞特蒂亚公司(FTDI)进一步巩固了其在USB接口集成电路产品的地位.此款多功能的单通道USB转UART/FIFO接口设备可通过EEPROM配置为各种 ...

  2. 用最简单的例子理解模板方法模式(Template Method Pattern)

    假设要做一道红烧肉,做法有很多,在不同的做法中都有相同的部分,比如都要放油.放肉.放调料等.也有不同之处,比如有些做法放可乐,有些做法放甜蜜酱,等等. 先提炼出一个抽象类,该类不仅有制作红烧肉的各个步 ...

  3. MVC文件上传09-使用客户端jQuery-File-Upload插件和服务端Backload组件让每个用户有专属文件夹,并在其中创建分类子文件夹

    为用户创建专属上传文件夹后,如果想在其中再创建分类子文件夹,该怎么做?可以在提交文件的视图中再添加一个隐藏域,并设置 name="uploadContext". 相关兄弟篇: MV ...

  4. ios 中是否每一个对象(尤其是在使用多线程时),都要判断一下对象是否为nil,以防止程序闪退?

    如下所示: Class messageClass = (NSClassFromString(@"MFMessageComposeViewController")); if (mes ...

  5. PHP 7.0 5.6 下安裝 phpLDAPadmin 发生错误的修正方法

    在稍具規模的網路環境中, 網管時常選用 LDAP 來進行帳號的統整管理, 一方面提供管理便利度, 另一方面使用者也不必因為不同系統而記憶不同帳號, phpLDAPadmin 是一套常見的 LDAP 管 ...

  6. [翻译] 极具动感的 FRDLivelyButton

    FRDLivelyButton https://github.com/sebastienwindal/FRDLivelyButton FRDLivelyButton is a simple UIBut ...

  7. [14] 齿轮(Gear Wheel)图形的生成算法

    顶点数据的生成 bool YfBuildGearwheelVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices ...

  8. [13] 弧面(Arc)图形的生成算法

    顶点数据的生成 bool YfBuildArcVertices ( Yreal radius, Yreal degree, Yreal height, Yuint slices, Yuint stac ...

  9. (转)看懂UML类图和时序图

    从一个示例开始 请看以下这个类图,类之间的关系是我们需要关注的: 车的类图结构为<<abstract>>,表示车是一个抽象类: 它有两个继承类:小汽车和自行车:它们之间的关系为 ...

  10. Python中filter、map、reduce、lambda 的用法

    Python内置了一些非常有趣但非常有用的函数,充分体现了Python的语言魅力! filter(function, sequence):对sequence中的item依次执行function(ite ...