android开发:使用Retrofit2框架,如果上传图片+json参数?
来源:互联网 发布:迪姆软件 编辑:程序博客网 时间:2024/06/06 11:01
功能需求
根据下面的post请求参数,用retrofit2框架接口请求
- 请求地址
http://{domain}/rest/services/file - 请求方法
POST - 请求数据
json数据格式如下:
{"name": "", // 文件名称"path": "" // 文件上传保存的路径,路径以“/”开始写起,路径最后无需再加“/”。}
4.响应状态码
实现代码
/** * 请求网络的API接口类 * Created by zzf on 17/10/08. */public interface ApiService {@Multipart @POST("/rest/services/file") Call<BaseResponse<String>> uploadFileWithRequestBody(@Part("json") RequestBody jsonBody, @Part MultipartBody.Part file);}
public class RetrofitUtil { private static RetrofitUtil mInstance; private Retrofit retrofit; public RetrofitUtil(){ } public static RetrofitUtil getInstance(){ if (mInstance == null){ synchronized (RetrofitUtil.class){ mInstance = new RetrofitUtil(); } } return mInstance; } /** * 自定义异常,当接口返回的{@link Response#code}不为{@link # OK}时,需要跑出此异常 * eg:登陆时验证码错误;参数为传递等 */ public static class APIException extends Exception { public String code; public String message; public APIException(String code, String message) { this.code = code; this.message = message; } @Override public String getMessage() { return message; } } public <T>T createRetrofitService(final Class<T> service,String baseUrl) { if(retrofit == null){ retrofit = new Retrofit.Builder() .client(getOkHttpClient())//指定网络执行器 .addConverterFactory(GsonConverterFactory.create())//指定 Gson 作为解析Json数据的Converter .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//指定使用RxJava 作为CallAdapter .baseUrl(baseUrl) .build(); } return retrofit.create(service); } public OkHttpClient getOkHttpClient() { //日志显示级别 分为4类:NONE、BASIC、HEADERS、BODY。 HttpLoggingInterceptor.Level level= HttpLoggingInterceptor.Level.BODY; //新建log拦截器 HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { @Override public void log(String message) { Log.d("zcb","OkHttp====Message:"+message); } }); loggingInterceptor.setLevel(level); //定制OkHttp OkHttpClient.Builder httpClientBuilder = new OkHttpClient .Builder(); //OkHttp进行添加拦截器loggingInterceptor // httpClientBuilder.addInterceptor(new HeaderInterceptor()); httpClientBuilder.addInterceptor(loggingInterceptor); return httpClientBuilder.build(); }}
/** * Created by ljd on 3/25/16. */public class ApiHelper extends RetrofitUtil { private ApiService apiService; private static ApiHelper mInstance; public static MediaType mediaType = MediaType.parse("application/json;charset=utf-8"); private final int pageSize = 10; /** * 服务器地址 */ private static final String API_BASE = Constants.API_BASE; public ApiService getApiService() { if (apiService == null) { apiService = createRetrofitService(ApiService.class,API_BASE); } return apiService; } private static class SingleHolder{ } public static ApiHelper getInstance(){ if (mInstance == null){ synchronized (ApiHelper.class){ mInstance = new ApiHelper(); } } return mInstance; }}
测试代码
public void upLoadPicToNet(String PictureUrl) { File file = new File(PictureUrl); String path ="/image/desk/product"; Gson gson = new Gson(); JsonObject jObj = new JsonObject(); jObj.addProperty("name","20171108105505.jpg"); jObj.addProperty("path",path); RequestBody bodyjson = RequestBody.create(ApiHelper.mediaType, gson.toJson(jObj)); final RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpg"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile); Call<BaseResponse<String>> baseResponseCall = ApiHelper.getInstance().getApiService().uploadFileWithRequestBody(bodyjson, body); baseResponseCall.enqueue(new Callback<BaseResponse<String>>() { @Override public void onResponse(Call<BaseResponse<String>> call, Response<BaseResponse<String>> response) { if(response.isSuccessful()){ Log.i(TAG,"上传成功啦"+response.toString()); }else { Log.i(TAG,"上传失败啦"); } } @Override public void onFailure(Call<BaseResponse<String>> call, Throwable t) { Log.e(TAG,"上传失败"); } }); }
阅读全文
0 0
- android开发:使用Retrofit2框架,如果上传图片+json参数?
- Android Rxjava+Retrofit2 多图片+文字上传
- Retrofit2.0 上传图片
- retrofit2.0,上传图片
- retrofit2上传图片
- Rxjava2+Retrofit2上传图片加参数失败问题解决
- android retrofit2.0框架的使用介绍
- Android Retrofit2 Post请求添加Json类型参数笔记
- Android前端RxJava2+Retrofit2;后端SpringMvc实现图片上传
- Retrofit2 使用,文件上传
- retrofit2上传文件参数问题
- 多参数多图片上传到服务器 如果不是使用afnetwoking
- retrofit2 上传多张图片
- retrofit2框架基本使用
- android使用json数据格式同时上传图片和文本
- android开发_表单上传图片及其它参数
- RxJava+Retrofit2结合使用mvp模式开发框架
- android使用AsyncHttpClient上传图片以及参数到服务器
- 2. Python脚本学习笔记二基本命令畅玩
- crontab要注意的点都在这了
- git: non-fast-forward errors
- python大作战之文件的读写操作(初学片)
- 5.4
- android开发:使用Retrofit2框架,如果上传图片+json参数?
- iOS navigationBar的一些知识点
- Centos下tensorflow环境搭建
- HDU 2037 今年暑假不AC(贪心)
- 自动售票系统 自动存取款机系统 java基础题
- 3. Python脚本学习笔记三字符串
- Frequent hit problems of Django on Linux
- FFT倒序算法—雷德Rader算法及matlab实现
- 16进制浮点数相互转换