upload image to server in android using multipart okhttp

 Upload image to server using okhttp :

        


Why do we use OkHttp in Android?

OkHttp is a third-party library developed by Square for sending and receive HTTP-based network requests. It is built on top of the Okio library, which tries to be more efficient about reading and writing data than the standard Java I/O libraries by creating a shared memory pool.
Step -1; show dialog box until data not show on Screen :
ProgressDialog dialogr;
dialogr = new ProgressDialog(currentActivity);
dialogr.setMessage("Please wait.....");
dialogr.setCancelable(false);
dialogr.setCanceledOnTouchOutside(false);
dialogr.show();
// Code for okhttp hit  request :
OkHttpClient client = new OkHttpClient();

String url = "https://pass website URL/InsertProductImage";

RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file))
.build();

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

client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(@NotNull okhttp3.Call call, @NotNull IOException e) {

new Thread() {
public void run() {
AddBlogAuthorActivity.this.runOnUiThread(new Runnable() {
public void run() {
dialogr.dismiss();
Toast.makeText(AddBlogAuthorActivity.this, e.getMessage().toString(),
Toast.LENGTH_SHORT).show(); }
});
}
}.start();


}

@Override
public void onResponse(@NotNull okhttp3.Call call, @NotNull okhttp3.Response response) throws IOException {

if (response.isSuccessful()) {
try {
String jsonData = response.body().string();
Log.v("dadadada", jsonData);
JSONObject jsonObject = new JSONObject(jsonData);
final String status = jsonObject.getString("Status");
if (status.equals("1")) {
dialogr.dismiss();
image1 = jsonObject.getString("Imagename");

new Thread() {
public void run() {
AddBlogAuthorActivity.this.runOnUiThread(new Runnable() {
public void run() {

Toast.makeText(AddBlogAuthorActivity.this,
"successfully Upload :- " + image1, Toast.LENGTH_SHORT).show(); }
});
}
}.start();
} else if (status.equals("-1")) {

new Thread() {
public void run() {
AddBlogAuthorActivity.this.runOnUiThread(new Runnable() {
public void run() {
dialogr.dismiss();
Toast.makeText(AddBlogAuthorActivity.this
, "Sorry, an error has occurred. Please try again later.",
Toast.LENGTH_SHORT).show();
//Do your UI operations like dialog opening or Toast here
}
});
}
}.start();
}

} catch (JSONException e) {
dialogr.dismiss();
e.printStackTrace();
}


}

}
});
} else {

alert(currentActivity, getResources().getString(R.string.alert_message_no_network),
getResources().getString(R.string.alert_message_no_network), getResources().getString(R.string.alert_ok_button_text_no_network), getResources().getString(R.string.alert_cancel_button_text_no_network),
true, false, ALERT_TYPE_NO_NETWORK);
}


}


Rajeshbhatt12

My name is Rajesh Bhatt. I am working as a senior android developer . I have created this blog for kotlin ,java and Android Development interview questions etc..

Previous Post Next Post