Retrofit實現圖文上傳至伺服器,retrofit圖文上傳

來源:互聯網
上載者:User

Retrofit實現圖文上傳至伺服器,retrofit圖文上傳

Retrofit實現圖文上傳至伺服器

 

前言:現在大多數的項目中都涉及圖片+文字上傳了,下面請詳見實現原理:

 

開發環境: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);    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.