Android okHttp上传图片
生活随笔
收集整理的這篇文章主要介紹了
Android okHttp上传图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天,簡單講講Android 使用OK HTTP上傳圖片。
?
不廢話了,直接上代碼:?
/*** 上傳圖片* @param url* @param imagePath 圖片路徑* @return 新圖片的路徑* @throws IOException* @throws JSONException*/ public static String uploadImage(String url, String imagePath) throws IOException, JSONException {OkHttpClient okHttpClient = new OkHttpClient();Log.d("imagePath", imagePath);File file = new File(imagePath);RequestBody image = RequestBody.create(MediaType.parse("image/png"), file);RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", imagePath, image).build();Request request = new Request.Builder().url(url).post(requestBody).build();Response response = okHttpClient.newCall(request).execute();JSONObject jsonObject = new JSONObject(response.body().string());return jsonObject.optString("image"); }?
直接在okHttp時調用函數就可以了。
?
Android okHttp上傳圖片就講完了。
?
就這么簡單。
總結
以上是生活随笔為你收集整理的Android okHttp上传图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android sharedprefer
- 下一篇: Android httpUrlConne