File upload with okhttp

前端 未结 4 1599
孤城傲影
孤城傲影 2020-12-06 02:54

Im finishing this project which is using okhttp for communication with a webservice.

All is going fine for regular GETs and POSTs, but I\'m not being able to properl

相关标签:
4条回答
  • 2020-12-06 03:16

    I just changed addFormDataPart instead addPart and Finally solved My Problem Using following code:

      /**
         * Upload Image
         *
         * @param memberId
         * @param sourceImageFile
         * @return
         */
        public static JSONObject uploadImage(String memberId, String sourceImageFile) {
    
            try {
                File sourceFile = new File(sourceImageFile);
    
                Log.d(TAG, "File...::::" + sourceFile + " : " + sourceFile.exists());
    
                final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    
                RequestBody requestBody = new MultipartBuilder()
                        .type(MultipartBuilder.FORM)
                        .addFormDataPart("member_id", memberId)
                        .addFormDataPart("file", "profile.png", RequestBody.create(MEDIA_TYPE_PNG, sourceFile))
                        .build();
    
                Request request = new Request.Builder()
                        .url(URL_UPLOAD_IMAGE)
                        .post(requestBody)
                        .build();
    
                OkHttpClient client = new OkHttpClient();
                Response response = client.newCall(request).execute();
                return new JSONObject(response.body().string());
    
            } catch (UnknownHostException | UnsupportedEncodingException e) {
                Log.e(TAG, "Error: " + e.getLocalizedMessage());
            } catch (Exception e) {
                Log.e(TAG, "Other Error: " + e.getLocalizedMessage());
            }
            return null;
        }
    
    0 讨论(0)
  • in OKHTTP 3+ use this AsyncTask

    SignupWithImageTask

      public class SignupWithImageTask extends AsyncTask<String, Integer, String> {
    
            ProgressDialog progressDialog;
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                progressDialog = new ProgressDialog(SignupActivity.this);
                progressDialog.setMessage("Please Wait....");
                progressDialog.show();
            }
    
            @Override
            protected String doInBackground(String... str) {
    
                String res = null;
                try {
    //                String ImagePath = str[0];
                    String name = str[0], email = str[1], dob = str[2], IMEI = str[3], phone = str[4], ImagePath = str[5];
    
                    File sourceFile = new File(ImagePath);
    
                    Log.d("TAG", "File...::::" + sourceFile + " : " + sourceFile.exists());
    
                    final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/*");
    
                    String filename = ImagePath.substring(ImagePath.lastIndexOf("/") + 1);
    
                    /**
                     * OKHTTP2
                     */
    //            RequestBody requestBody = new MultipartBuilder()
    //                    .type(MultipartBuilder.FORM)
    //                    .addFormDataPart("member_id", memberId)
    //                    .addFormDataPart("file", "profile.png", RequestBody.create(MEDIA_TYPE_PNG, sourceFile))
    //                    .build();
    
                    /**
                     * OKHTTP3
                     */
                    RequestBody requestBody = new MultipartBody.Builder()
                            .setType(MultipartBody.FORM)
                            .addFormDataPart("image", filename, RequestBody.create(MEDIA_TYPE_PNG, sourceFile))
                            .addFormDataPart("result", "my_image")
                            .addFormDataPart("name", name)
                            .addFormDataPart("email", email)
                            .addFormDataPart("dob", dob)
                            .addFormDataPart("IMEI", IMEI)
                            .addFormDataPart("phone", phone)
                            .build();
    
                    Request request = new Request.Builder()
                            .url(BASE_URL + "signup")
                            .post(requestBody)
                            .build();
    
                    OkHttpClient client = new OkHttpClient();
                    okhttp3.Response response = client.newCall(request).execute();
                    res = response.body().string();
                    Log.e("TAG", "Response : " + res);
                    return res;
    
                } catch (UnknownHostException | UnsupportedEncodingException e) {
                    Log.e("TAG", "Error: " + e.getLocalizedMessage());
                } catch (Exception e) {
                    Log.e("TAG", "Other Error: " + e.getLocalizedMessage());
                }
    
    
                return res;
    
            }
    
            @Override
            protected void onPostExecute(String response) {
                super.onPostExecute(response);
                if (progressDialog != null)
                    progressDialog.dismiss();
    
                if (response != null) {
                    try {
    
                        JSONObject jsonObject = new JSONObject(response);
    
    
                        if (jsonObject.getString("message").equals("success")) {
    
                            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
    
                            SharedPreferences settings = getSharedPreferences("preference", 0); // 0 - for private mode
                            SharedPreferences.Editor editor = settings.edit();
                            editor.putString("name", jsonObject1.getString("name"));
                            editor.putString("userid", jsonObject1.getString("id"));
                            editor.putBoolean("hasLoggedIn", true);
                            editor.apply();
    
                            new UploadContactTask().execute();
    
                            startActivity(new Intent(SignupActivity.this, MainActivity.class));
                        } else {
                            Toast.makeText(SignupActivity.this, "" + jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(SignupActivity.this, "Something Went Wrong", Toast.LENGTH_SHORT).show();
                }
    
            }
        }
    
    0 讨论(0)
  • 2020-12-06 03:33

    Here is how to upload file using okhttp3.

          try {
    
              UpdateInformation("yourEmailAddress", filePath, sourceFile);
    
              } catch (IOException e) {
                            e.printStackTrace();
             }
    
        private void UploadInformation(String email, final String _filePath, final File file) throws IOException {
    
    
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
    
    
                //show progress bar here
    
                }
            });
    
    
            OkHttpClient client = new OkHttpClient.Builder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .build();
    
    
    
    
    
            String mime = getMimeType(_filePath);
    
    
            RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("file", file.getName(),
                            RequestBody.create(MediaType.parse(mime), file))
                    .addFormDataPart("email", email)
                    .build();
    
    
    
    
    
            okhttp3.Request request = new okhttp3.Request.Builder()
                    .url("yourEndPointURL")
                    .post(body)
                    .addHeader("authorization", "yourEndPointToken")
                    .addHeader("content-type", "application/json")
                    .build();
    
    
    
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    call.cancel();
    
    
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
    
                        //hide progress bar here
    
                        }
                    });
    
                }
    
                @Override
                public void onResponse(Call call, okhttp3.Response response) throws IOException {
    
    
                    try {
    
                        final String myResponse = response.body().string();
    
    
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
    
                        //hide progress bar here
    
                        //Cont from here
                        //Handle yourEndPoint Response.
    
    
    
                            }
                        });
    
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
    
                }
    
    
    
            });
        }
    
    
    private String getMimeType(String path) {
            FileNameMap fileNameMap = URLConnection.getFileNameMap();
            String contentTypeFor = fileNameMap.getContentTypeFor(path);
            if (contentTypeFor == null)
            {
                contentTypeFor = "application/octet-stream";
            }
            return contentTypeFor;
        }
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-06 03:34

    I found answer to my own question a bit after initial post.

    I´ll leave it here, because it can be useful to others, given that there is such a few okhttp upload examples around:

    RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
            .addFormDataPart("group", getGroup())
            .addFormDataPart("type", getType())
            .addFormDataPart("entity", Integer.toString(getEntity()))
            .addFormDataPart("reference", Integer.toString(getReference()))
            .addFormDataPart("task_file", "file.png", RequestBody.create(MediaType.parse("image/png"), getFile()))
                                                    .build();
    

    There is no reason to use "addPart" with "Headers.of" etc like in the recipes, addFormDataPart does the trick.

    And for the file field itself, it takes 3 arguments: name, filename and then the file body. That's it.

    0 讨论(0)
提交回复
热议问题