0.下载文件

1.gradle 添加

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okio:okio:1.9.0'

2.AndroidManifest.xml 权限   

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

3.Application中初始化

HttpUtil.getInstance().init(this, "http://baiqi.ej-cloud.com:8070/iot/");

4.调用

                HttpUtil.getInstance().get(
new HttpUtil.Builder()
.url("user/token/init")
.params("key", "1")
.params("type", "4")
.callBackSuccess(new CallBackSuccess() {
@Override
public void onSuccess(String json) {
TopicResponse topicResponse = JsonUtil.fromJson(json, TopicResponse.class);
tv.setText(topicResponse.data.id);
}
}));

5.添加上传图片功能 HttpUtil.java文件中

private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
public void uploadImages(List<String> selectedImages, String url) {
// mImgUrls为存放图片的url集合
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (int i = 0; i < selectedImages.size(); i++) {
File f = new File(selectedImages.get(i));
if (f != null) {
builder.addFormDataPart("img" + i, f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f));
}
} MultipartBody requestBody = builder.build();
//构建请求
Request request = new Request.Builder()
.url(url)//地址
.post(requestBody)//添加请求体
.build();
okHttpClient.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(okhttp3.Call call, IOException e) {
Log.wtf(TAG, "request " + call.request().url());
Log.wtf(TAG, "上传失败:e.getLocalizedMessage() = " + e.getLocalizedMessage());
} @Override
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
Log.wtf(TAG, "request " + call.request().url());
Log.wtf(TAG, "上传照片成功:response = " + response.body().string());
}
});
}

调用

HttpUtil.getInstance().uploadImages(selectedImages, "http://baiqi.ej-cloud.com/iotlife/user/opinion/imgUpload&key=1&image=uicon&token="
+token+"&feedbackId" +feedbackId );

参考:

https://github.com/gengqiquan/HttpUtil

https://github.com/hongyangAndroid/okhttputils

OkHttp3 + retrofit2 封装的更多相关文章

  1. okHttp3自用封装

    okHttp都已经出到3.2.0了,现在才开始要用到它,感觉自己好low~~ 根据平时自己的习惯,还是自己做一下封装,让代码撸起来更加顺畅一点! okhttp-3.2.0和okio-1.7.0就不多说 ...

  2. okhttp3 get post 简单封装

    最近打算在新项目中使用 okhttp3, 简单封装了一下异步 get post 因为 CallBack 也是在子线程中执行,所以用到了 Handler public class MyOkHttpCli ...

  3. 封装OkHttp,通过Callback改造Callback实现

    1:实现Callback回调接口import android.os.Handler;import android.os.Looper;import android.os.Message;import ...

  4. 封装OkHttp,通过改造Callback接口实现

    1:实现Callback回调接口import android.os.Handler;import android.os.Looper;import android.os.Message; import ...

  5. Android中的设计模式之观察者模式

    参考 <设计模式:可复用面向对象软件的基础 >5.7 Observer 观察者 对象行为型模式 <设计模式解析> 18.4 Observer模式 <Android源码设计 ...

  6. 【从零开始撸一个App】PKCE

    一个成功的App背后肯定有一堆后端服务提供支撑,认证授权服务(Authentication and Authorization Service,以下称AAS)就是其中之一,它是约束App.保障资源安全 ...

  7. 【从零开始撸一个App】Kotlin

    工欲善其事必先利其器.像我们从零开始撸一个App的话,选择最合适的语言是首要任务.如果你跟我一样对Java蹒跚的步态和僵硬的语法颇感无奈,那么Kotlin在很大程度上不会令你失望.虽然为了符合JVM规 ...

  8. retrofit2 okhttp3 RxJava butterknife 示例

    eclipse的jar包配置 eclipse中貌似用不了butterknife buildToolsVersion "23.0.2" defaultConfig { applica ...

  9. Rxjava+Retrofit2+Okhttp3多文件上传(服务器端代码+客户端代码)

    所有代码亲测可用,如有问题,欢迎指正. 首先在ApiService接口文件中新建文件上传接口 public interface ApiService { static final String BAS ...

随机推荐

  1. macOS Mojave 10.14 无法安装brew缺少Command Line Tools for Xcode的解决办法

    问题描述: 首先我的版本是 Xcode 10.1 如果按照以前的方法安装brew 复制 1 /usr/bin/ruby -e "$(curl -fsSL https://raw.github ...

  2. 安装 PostgreSQL 时丢失 libintl-8.dll 解决方案

     发表于 2013 年 11 月 13 日     修订于 2018 年 05 月 05 日 PostgreSQL 比 MySQL 有更多的高级特性,而且微信支付的数据库也是基于 PostgreSQL ...

  3. OLAP、OLTP的介绍CBO/RBO

    OLTP与OLAP的介绍 数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical ...

  4. 13Flutter页面布局 Wrap组件

    /* Flutter页面布局Wrap组件: Wrap可以实现流布局,单行的Wrap跟Row表现几乎一致,单列的Wrap则跟Row表现几乎一致. 但Row与Column都是单行单列的.Wrap则突破了这 ...

  5. double,float,BigDecimal类型数值的操作

    float四舍五入保留两位小数 /** * float四舍五入保留两位小数 * */ public static float formatDecimal(float n) { return (Math ...

  6. Example config file /etc/vsftpd.conf

    # Example config file /etc/vsftpd.conf # # The default compiled in settings are fairly paranoid. Thi ...

  7. seaborn可视化

    文章来自https://blog.csdn.net/qq_33120943/article/details/76569756 详细教程可以查看官方额示例:http://seaborn.pydata.o ...

  8. CentOS7.2环境下安装Nginx

    目录 1.下载安装包 2.安装nginx 3.配置nginx开机启动 4.测试 1.下载安装包 官网下载 wget http://nginx.org/download/nginx-1.16.0.tar ...

  9. WordPress自定义循环

    我们在学WordPress的时候,最常用到的就是循环了.写模板的时候,多数的时间都是和循环打交道的.如果你不能很详细的了解WordPress的循环,是很难写出模板来的. 而WordPress自定义循环 ...

  10. Spark On YARN(Yarn-Cluster模式)启动流程源码分析(二)

    转自:https://www.cnblogs.com/yy3b2007com/p/11087180.html 本章将针对yarn-cluster(--master yarn –deploy-mode ...