欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Retrofit实现图文上传至服务器

时间:2022-01-03 12:02:28|栏目:Android代码|点击:

前言:现在大多数的项目中都涉及图片+文字上传了,下面请详见实现原理:

开发环境:AndroidStudio

1.引入依赖:

compile 'com.squareup.retrofit2:retrofit:2.1.0'  

2.网络权限:

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

3.创建上传对象OkHttpClient :

private static final OkHttpClient client = new OkHttpClient.Builder()
   .addInterceptor(new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
     Request request = chain
       .request()
       .newBuilder()
       .build();
     return chain.proceed(request);
    }
   })
   .readTimeout(10, TimeUnit.SECONDS)//设置读取超时时间
   .writeTimeout(10, TimeUnit.SECONDS)//设置写的超时时间
   .connectTimeout(15, TimeUnit.SECONDS)//设置连接超时时间
   .build();

4.上传图片的公有方法:

private synchronized final static void uploadImgAndParameter(Map<String, Object> map, String url,
   final UIDataListener listener) {
  // mImgUrls为存放图片的url集合
  MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
  if (null != map) {
   for (Map.Entry<String, Object> entry : map.entrySet()) {
    if (entry.getValue() != null) {
     if (entry.getValue() instanceof File) {
      File f = (File) entry.getValue();
      builder.addFormDataPart(entry.getKey(), f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f));
     } else {
      builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
     }
    }
   }
  }
  //创建RequestBody
  RequestBody body = builder.build();

//  MultipartBody requestBody = builder.build();
  //构建Request请求
  final Request request = new Request.Builder()
    .url(url)//地址
    .post(body)//添加请求体
//    .post(requestBody)//添加请求体
    .build();
  client.newCall(request).enqueue(new okhttp3.Callback() {
   @Override
   public void onResponse(Call call, final Response response) throws IOException {
    if (response.isSuccessful()) {//判断是否成功
     final String data = response.body().string();//string()仅可调用一次。否则报IllegalStateException: closed异常
     Log.i("file1", "上传照片成功-->" + data);
     onSuccess(listener, data);
     call.cancel();//上传成功取消请求释放内存
    }
   }
   @Override
   public void onFailure(Call call, final IOException e) {
    Log.i("file2", "上传失败-->" + e.getMessage());
    String msg = e.getMessage();
    if (msg == null || msg.equals("timeout")) {
     onError(listener, "网络不稳定请求超时!");
    } else {
     onError(listener, e.getMessage());
    }
    call.cancel();//上传失败取消请求释放内存
   }
  });
 }

//注意:添加手机图片,别忘了添加SD卡权限

5.全部代码:

public class HttpUtil {
  private static final Handler handler = new Handler(Looper.getMainLooper());
  private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/*");
  private static final OkHttpClient client = new OkHttpClient.Builder()
      .addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
          Request request = chain
              .request()
              .newBuilder()
              .build();
          return chain.proceed(request);
        }
      })
      .readTimeout(10, TimeUnit.SECONDS)//设置读取超时时间
      .writeTimeout(10, TimeUnit.SECONDS)//设置写的超时时间
      .connectTimeout(15, TimeUnit.SECONDS)//设置连接超时时间
      .build();
  /**
   * 实例--》添加商品
   */
  public static void addCoupon( int shopperId,String shopperName,
                 File file, final UIDataListener listener) {
    String url = "shopappajx/shopAppCouponAction_saveCoupon.htm";
    Map<String, Object> map = new HashMap<>();
    map.put("shopperId", shopperId);
    map.put("shopperName", shopperName);
    map.put("couponImage", file);//商品图片
    uploadImgAndParameter(map, url, listener);
  }
  //上传图片共有方法
  private synchronized final static void uploadImgAndParameter(Map<String, Object> map, String url,
      final UIDataListener listener) {
    // mImgUrls为存放图片的url集合
    MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
    if (null != map) {
      for (Map.Entry<String, Object> entry : map.entrySet()) {
        if (entry.getValue() != null) {
          if (entry.getValue() instanceof File) {
            File f = (File) entry.getValue();
            builder.addFormDataPart(entry.getKey(), f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f));
          } else {
            builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
          }
        }
      }
    }
    //创建RequestBody
    RequestBody body = builder.build();

//    MultipartBody requestBody = builder.build();
    //构建Request请求
    final Request request = new Request.Builder()
        .url(url)//地址
        .post(body)//添加请求体
//        .post(requestBody)//添加请求体
        .build();
    client.newCall(request).enqueue(new okhttp3.Callback() {
      @Override
      public void onResponse(Call call, final Response response) throws IOException {
        if (response.isSuccessful()) {//判断是否成功
          final String data = response.body().string();//string()仅可调用一次。否则报IllegalStateException: closed异常
          Log.i("file1", "上传照片成功-->" + data);
          onSuccess(listener, data);
          call.cancel();//上传成功取消请求释放内存
        }
      }
      @Override
      public void onFailure(Call call, final IOException e) {
        Log.i("file2", "上传失败-->" + e.getMessage());
        String msg = e.getMessage();
        if (msg == null || msg.equals("timeout")) {
          onError(listener, "网络不稳定请求超时!");
        } else {
          onError(listener, e.getMessage());
        }
        call.cancel();//上传失败取消请求释放内存
      }
    });
  }
  private final static void onSuccess(final UIDataListener listener, final String data) {
    handler.post(new Runnable() {
      public void run() {
        // 需要在主线程的操作。
        listener.onSuccess(data);
      }
    });
  }
  private final static void onError(final UIDataListener listener, final String msg) {
    if (null != listener) {
      handler.post(new Runnable() {
        public void run() {
          // 需要在主线程的操作。
          listener.onFailure(msg);
        }
      });
    }
  }
  public interface UIDataListener {
   //网络请求成功
    void onSuccess(String data);
   //网络请求失败
    void onFailure(String errorMassage);
  }
}

上一篇:Android ListView实现下拉顶部图片变大效果

栏    目:Android代码

下一篇:浅谈Android安全风险与防范措施

本文标题:Retrofit实现图文上传至服务器

本文地址:http://www.codeinn.net/misctech/188929.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有