Retrofit2を使用してギャラリー画像をサーバーにアップロードします。 APIにはフォームデータとしてflieを含む1つのパラメータがあります&別のパラメータはキーのあるヘッダです。サーバーにイメージをアップロードするときに、サーバーからファイルが見つかりませんというメッセージが表示されます。私は適切なファイル名を送信していますが、なぜそれが与えられているのかわかりません。誰も私が解決するのを助けることができますか。下は私のコードです。Retrofit2 POSTリクエストを使用してサーバーに画像をアップロード
final ProgressDialog progressDialog;
progressDialog = new ProgressDialog(UserProfileActivity.this);
progressDialog.setMessage("Updating Data");
progressDialog.show();
//Create Upload Server Client
ApiService service = RetroClient.getApiService();
//File creating from selected URL
File file = new File(imgDecodableString);
Log.d("TAG","file="+file.getName());
// create RequestBody instance from file
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("image", file.getName(), requestFile);
Call<com.example.datamodel.Response> resultCall = service.uploadImage(loginHash,body);
resultCall.enqueue(new Callback<com.example.datamodel.Response>() {
@Override
public void onResponse(Call<com.example.datamodel.Response> call, retrofit2.Response<com.example.datamodel.Response> response) {
com.example.datamodel.Response mBean = response.body();
if(null != mBean)
{
Log.d("TAG","RS="+mBean.getMessage());
Log.d("TAG","RS="+mBean.getData().getvImage());
}
progressDialog.dismiss();
}
@Override
public void onFailure(Call<com.example.datamodel.Response> call, Throwable t) {
progressDialog.dismiss();
Log.d("TAG", "fail=" + t.getMessage());
}
});
RetroClient.java
public class RetroClient {
public RetroClient() {
}
private static Retrofit getRetroClient() {
return new Retrofit.Builder()
.baseUrl(AppConstants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static ApiService getApiService() {
return getRetroClient().create(ApiService.class);
}}
ApiService.java logcatで
public interface ApiService {
@Multipart
@POST("api/User/uploadUserProfilePhoto")
Call<Response> uploadImage(@Header("vLoginHash") String vLoginHash,@Part MultipartBody.Part file);}
私は取得しています、ファイルサーバーからのメッセージが見つかりません。誰でも助けてください。前もって感謝します。マルチパート
OnActivityREsult if (resultCode == Activity.RESULT_OK) {
Bitmap photo = null;
if(requestCode==REQUEST_GALLERY){
try {
photo = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
if(photo!=null){
Uri tempUri = getImageUri(getActivity(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
apimy.upload(header value,requestBody).enque...
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
で
@POST("/api/name.jpg")
Call<Object> upload(
@Header("header_name") String authorization,
@Body RequestBody body);
せずにこのコードは、私のために正常に動作します
これは助けになるかもしれませんhttp://stackoverflow.com/a/40608320/5305430 – sushildlh
@sushildlh私はすでにこれを行い、MediaType.parse( "image/*")も変更しますが、同じエラーを返します –