2

I am trying to upload an image to the my Server with POST command. For that I am Using this code from OkHttp Recipes.

private static final MediaType MEDIA_TYPE_JPG=MediaType.parse("image/jpg");
private final OkHttpClient client = new OkHttpClient();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_equipment);
    PATH_TO_EXTERNAL = Environment.getExternalStorageDirectory().getPath();
    FULL_PATH = PATH_TO_EXTERNAL + IMAGE_PATH;
    file = new File(FULL_PATH);
    body = RequestBody.create(MEDIA_TYPE_JPG, file);

    run(); /* Calling it on Background Thread*/ 
}


public void run() throws Exception {

    RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
            .addFormDataPart("aadcs", "acsd.jpg",body)
            .build();

    Request request = new Request.Builder().url(UPLOAD_URL).post(requestBody).build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) { throw new IOException("Unexpected code " + response); }

    Log.d("Response", response.body().string());
}

But the Problem is I am not able to get any response from this i.e when I debug the code line by line, it never goes beyond this line :
Response response = client.newCall(request).execute();.

Also the Url is working fine when I test it from any online REST CLients like POSTMAN. I am not able to figure out what I am doing wrong here.

HELP!!!

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
Silly Questions
  • 183
  • 1
  • 1
  • 12

0 Answers0