Image upload using okHttp

后端 未结 4 1227
無奈伤痛
無奈伤痛 2020-11-28 10:56

i want to upload image using okhttp but i am not able to find MultipartBuilder for Post Image.What can i use instead of this.

Here is my code

<
4条回答
  •  孤独总比滥情好
    2020-11-28 11:59

    Here is the multi part request class.

    import java.io.File;
    import java.io.IOException;
    
    import org.apache.http.HttpStatus;
    
    import android.content.Context;
    
    import com.esp.ro.util.Config;
    import com.esp.ro.util.Log;
    import com.squareup.okhttp.MediaType;
    import com.squareup.okhttp.MultipartBuilder;
    import com.squareup.okhttp.OkHttpClient;
    import com.squareup.okhttp.Request;
    import com.squareup.okhttp.RequestBody;
    import com.squareup.okhttp.Response;
    
    
    public class MultipartRequest {
    
    public Context caller;
    public MultipartBuilder builder;
    private OkHttpClient client;
    
    public MultipartRequest(Context caller) {
        this.caller = caller;
        this.builder = new MultipartBuilder();
        this.builder.type(MultipartBuilder.FORM);
        this.client = new OkHttpClient();
    }
    
    public void addString(String name, String value) {
        this.builder.addFormDataPart(name, value);
    }
    
    public void addFile(String name, String filePath, String fileName) {
        this.builder.addFormDataPart(name, fileName, RequestBody.create(
                MediaType.parse("image/jpeg"), new File(filePath)));
    }
    
    public void addTXTFile(String name, String filePath, String fileName) {
        this.builder.addFormDataPart(name, fileName, RequestBody.create(
                MediaType.parse("text/plain"), new File(filePath)));
    }
    
    public void addZipFile(String name, String filePath, String fileName)
    {
        this.builder.addFormDataPart(name, fileName, RequestBody.create(
               MediaType.parse("application/zip"), new File(filePath)));
    }
    
    public String execute(String url) {
        RequestBody requestBody = null;
        Request request = null;
        Response response = null;
    
        int code = 200;
        String strResponse = null;
    
        try {
            requestBody = this.builder.build();
            request = new Request.Builder().header("AUTH-KEY", Config.API_KEY)
                    .url(url).post(requestBody).build();
    
            Log.print("::::::: REQ :: " + request);
            response = client.newCall(request).execute();
            Log.print("::::::: response :: " + response);
    
            if (!response.isSuccessful())
                throw new IOException();
    
            code = response.networkResponse().code();
    
            if (response.isSuccessful()) {
                strResponse = response.body().string();
            } else if (code == HttpStatus.SC_NOT_FOUND) {
                // ** "Invalid URL or Server not available, please try again" */
                strResponse = caller.getResources().getString(
                        R.string.error_invalid_URL);
            } else if (code == HttpStatus.SC_REQUEST_TIMEOUT) {
                // * "Connection timeout, please try again", */
                strResponse = caller.getResources().getString(
                        R.string.error_timeout);
            } else if (code == HttpStatus.SC_SERVICE_UNAVAILABLE) {
                // *
                // "Invalid URL or Server is not responding, please try again",
                // */
                strResponse = caller.getResources().getString(
                        R.string.error_server_not_responding);
            }
        } catch (Exception e) {
            Log.error("Exception", e);
            Log.print(e);
        } finally {
            requestBody = null;
            request = null;
            response = null;
            builder = null;
            if (client != null)
                client = null;
            System.gc();
        }
        return strResponse;
      }
    }
    

    Hope this help you.

    Note : If you are using old OkHttp which is below version 3 then you can use this method.If you are using version 3 or above here is the answer for you.

提交回复
热议问题