其中使用了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. ASP.NET Web Api 实现数据的分页

    前言 这篇文章我们将使用不同的方式实现手动分页(关于高端大气上档次的OData本文暂不涉及,但有可能会在系列的后期介绍,还没确定...),对于分页的结果,我们将采用2种不同的方式响应给客户端(1.将分 ...

  2. oracle-systemtap

    https://github.com/hatem-mahmoud/scripts https://mahmoudhatem.wordpress.com/2015/05/26/stapora-v1-0- ...

  3. Running multiple instances of Xamarin Studio on a Mac

    I love developing software on my MacBook Air! I got the latest version with the maximum possible spe ...

  4. Matlab绘图控制命令

    [转载] 图形的控制与表现 (Figure control and representation) MATLAB提供的用于图形控制的函数和命令: axis:  人工选择坐标轴尺寸.     clf: ...

  5. 《Head First 设计模式》学习笔记——策略模型

    我们全都使用别人设计好的库与框架.我们讨论库与框架.利用他们的API编译成我们的程序.享受运用别人的代码所带来的长处.看看java api它所带来的功能:网络.GUI.IO等.库与框架长久以来,一直扮 ...

  6. linux 监控CPU 内存情况

    htop

  7. 列出Windows域中所有的机器

    我所在的部门大概管理了300+台Windows终端,最近开始采用域的方式来进行管理.(别笑我们土,原来手工修改Windows口令太痛苦了.) 现在的任务是想在域控服务器中列出纳入域管理的所有机器,以及 ...

  8. 【BZOJ】【2200】【USACO 2011 Jan】道路和航线

    做了一天…… TLE:数组开小了-_-#道路是有50000的,双向要乘二.(我特么怎么想的就以为是树了……) WA:一些大点都WA了,小点都过了.好纠结…… AC了QAQ,不知道为什么,在并查集合并的 ...

  9. is-subsequence

    public class Solution { public boolean isSubsequence(String s, String t) { int idx = 0; for (int i=0 ...

  10. Handler Thread 内部类引起内存泄露分析

    非静态内部类引起内存泄漏的原因 内部类的实现其实是通过编译器的语法糖(Syntactic sugar)实现的,通过生成相应的子类即以OutClassName$InteriorClassName命名的C ...