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例子来测试了.本文的顺序为: ...
随机推荐
- perf 工具介绍3
http://blog.chinaunix.net/uid-10540984-id-3854969.html http://blog.csdn.net/zhangskd/article/details ...
- 关于ANDROID模拟器的一些事
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 继上一篇Android Studio VS Eclipse的文章后接着来分享AnDevCo ...
- tinyxml使用笔记与总结
在TinyXML中,根据XML的各种元素来定义了一些类: TiXmlBase:整个TinyXML模型的基类. TiXmlAttribute:对应于XML中的元素的属性. ...
- json-lib包笔记
json-lib.jar开发包使用: 依赖包:commons-beanutils.jar;commons-httpclient.jar;commons-lang.jar;ezmorph.jar;不少人 ...
- C柔性数组
柔性数组成员 柔性数组 .允许结构中包含一个大小可变的数组,sizeof返回的这种结构大小不包括柔性数组的内存. .包含柔性数组成员的结构要使用malloc()函数进行内存的动态分配.分配的内存大于结 ...
- 实现Hadoop的Writable接口Implementing Writable interface of Hadoop
As we saw in the previous posts, Hadoop makes an heavy use of network transmissions for executing it ...
- [转]C++之运算符重载(2)
上一节主要讲解了C++里运算符重载函数,在看了单目运算符(++)重载的示例后,也许有些朋友会问这样的问题.++自增运算符在C或C++中既可以放在操作数之前,也可以放在操作数之后,但是前置和后置的作用又 ...
- eclipse oracle Database Manager plugin oracle数据库连接插件
在eclipseIDE中开发项目需要经常连接oracle数据库,装一些oracle client 去连接数据库,界面来回切换比较麻烦,所以专门找了一个eclipse插件专门连接oracle,跟大家分享 ...
- [Linux]在终端启动程序关闭终端不退出的方法
一般情况下关闭终端时,那么在这个终端中启动的后台程序也会终止,要使终端关闭后,后台程序保持执行,使用这个指令: nohup 命令 & 如:nohup test.sh & 回车,然后提示 ...
- [置顶] think in java interview-高级开发人员面试宝典(一)
“生死六重门” 无论你是在职,非在职,高级工程师,工程师,架构师,如果你正在面试阶段,请看完此文! 相信这篇文章对你的职业生涯和阶值观会造成重大的改变! 如果你是一名PM或者是管理者正在物色合适的开发 ...