php - Image uploading API Call works properly with Postman but why not working with Android App ?
Get the solution ↓↓↓Solution:
Because there's something wrong with your code about multipart..
Interface :
@Multipart
@POST("services.php?apicall=profileImage")
Call<ResponseBody> postImage(
@Part MultipartBody.Part image,
@Part("id") RequestBody id
);
Call the API
Retrofit retrofit=new Retrofit.Builder().
baseUrl(MyConstants.ROOT_URL).
addConverterFactory(ScalarsConverterFactory.create()).
build();
apinterface services = retrofit.create(apinterface.class);
//Bikin request body
File file = new File(mediaPath);
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), reqFile);
RequestBody reqId = RequestBody.create(MediaType.parse("text/plain"), uid);
//Tembak APInya
services.postImage(reqfile, reqId)
.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//tulis aksi disini
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
//tulis aksi disini
}
});
Answer
Solution:
Change the post method like this.
@Multipart
@POST(Urls.POST_A_QUERY)
Call<CommonResponseModel> postAQuery(@Part("treatmentDetails") RequestBody treatmentDetails,@Part MultipartBody.Part file1);
Then
File file = new File(filePath);
RequestBody requestFile = RequestBody.create(MediaType.parse(mediaType), file);
document1 = MultipartBody.Part.createFormData("report1", Utils.getEdittextData(edtFirstFileName).concat(".jpg"), requestFile);
Then pass that document1 value in second parameter.
String mediaType = getActivity().getContentResolver().getType(uri);
if (mediaType == null) {
mediaType = Utils.getMimeType(filePath);
}
Get mimetype function.
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.