1. 程式人生 > >Android okhttp上傳圖片檔案

Android okhttp上傳圖片檔案

之前在開發當中使用過的網路請求框架Volley、Xutils、async http等,而最近目前最流行的應該是okhttp,網上有很多封裝好的工具類,但是發現對圖片檔案上傳的不多,我也是第一次使用okhttp,專案當中有多圖片上傳的需求,而網上給出的工具類幾乎沒有對圖片檔案處理的,最後參考別人的例子才發現媒體檔案上傳需要重寫MultipartBody這個類,其實我發現在之前的專案中使用的volley框架也是如此,對於okhttp上傳圖片我是這麼寫的:
 Map<String, String> params=new HashMap<>();
               //引數
               params.put
("uid", BaseAppliction.newInstance().getBaseSp().getUid() + ""); params.put("task_id", GetID.taskid + ""); params.put("task_order", GetID.task_order + ""); params.put("utype", BaseAppliction.newInstance().getBaseSp().getUtype() + ""); params.put
("remarks", ed_message.getText().toString()); //重寫MultipartBody類 MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder(); multipartBodyBuilder.setType(MultipartBody.FORM); //遍歷map中所有引數到builder,為什麼會有折磨多的if判斷呢,因為後臺不支援陣列和集合上傳所以只能一個個的加判斷了,哎都是淚 //引數說明:GetImageFile裡面存放的是圖片的路徑,img1,img2,img3:分別代表第一二三張圖片的引數 if (params != null) { for (String key : params.keySet
()) { multipartBodyBuilder.addFormDataPart(key, params.get(key)); } if (GetImageFile.list.size() == 1) {//如果上傳一張 if (GetImageFile.list.get(0) != null) { File f = new File(GetImageFile.list.get(0)); multipartBodyBuilder.addFormDataPart("img1", f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f)); } } if (GetImageFile.list.size() == 2) {//如果上傳兩張 if (GetImageFile.list.get(0) != null & GetImageFile.list.get(1) != null) { File f = new File(GetImageFile.list.get(0)); multipartBodyBuilder.addFormDataPart("img1", f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f)); File f1 = new File(GetImageFile.list.get(1)); multipartBodyBuilder.addFormDataPart("img2", f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f1)); } } if (GetImageFile.list.size() == 3) {//如果上傳三張 if (GetImageFile.list.get(0) != null & GetImageFile.list.get(1) != null & GetImageFile.list.get(2) != null) { File f = new File(GetImageFile.list.get(0)); multipartBodyBuilder.addFormDataPart("img1", f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f)); File f1 = new File(GetImageFile.list.get(1)); multipartBodyBuilder.addFormDataPart("img2", f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f1)); File f2 = new File(GetImageFile.list.get(2)); multipartBodyBuilder.addFormDataPart("img3", f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f2)); } } } //構建請求體 RequestBody requestBody = multipartBodyBuilder.build(); Request.Builder RequestBuilder = new Request.Builder(); RequestBuilder.url(url);// 新增URL地址 RequestBuilder.post(requestBody); Request request = RequestBuilder.build(); OkHttpClient mOkHttpClient = new OkHttpClient(); mOkHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { call.cancel(); } @Override public void onResponse(Call call, Response response) throws IOException { String str = response.body().string(); System.out.println(str); call.cancel(); /**注意為什麼會有一個Thread:這可是一個同步操作,不是非同步的啊,假如你想在資料請求成功後,也就是表單提交成功後 * 你想 Toast.makeText(this,"上傳成功",Toast.LENGTH_SHORT).show(); 會直接丟擲異常,所以建立一個執行緒外邊接受handler * toast就行了 */ new Thread(new Runnable() { @Override public void run() { Message message = new Message(); message.what = tag; handler.sendMessage(message); } }).start(); } });