نحوه نوشتن endpoint و ارسال فایل *تاپیک بسته شد*

پرسیده شده
فعالیت 1242 روز پیش
دیده شده 521 بار
2

خب من توی پست من چیزی که دارم رو به صورت فایل ضمیمه میکنم
و اینکه endpoint خودم رو توی اینترفیس مربوطه به صورت زیر نوشتم :

    @Multipart
    @POST("employee/update-own-profile")
    Single<UpdateProfileResponse> updateProfile(
        @Part("name") RequestBody name,
        @Part("username") RequestBody username,
        @Part MultipartBody.Part image
    );

و اینکه توی ریپازیتوری به صورت زیر پیاده سازی کردم متد مربوط به این قسمت رو 

public Single<Boolean> updateOwnProfile(String fullName, String nationalCode, File imageFile) {

        RequestBody fullNameBody= RequestBody.create(MediaType.parse("text/plain"),fullName);
        RequestBody userNameBody= RequestBody.create(MediaType.parse("text/plain"),nationalCode);

        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"),imageFile);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("profile_pic",imageFile.getName(),requestBody);

        return apiService.updateProfile(fullNameBody,userNameBody,filePart).map(updateProfileResponse -> updateProfileResponse.getCode() == 200);
    }

اروری که میگیرم هم به صورت زیره

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.asenadev.sana, PID: 19890
    io.reactivex.exceptions.OnErrorNotImplementedException: The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: https://github.com/ReactiveX/RxJava/wiki/Error-Handling | java.io.FileNotFoundException: /external/images/media/78207: open failed: ENOENT (No such file or directory)
        at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
        at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
        at io.reactivex.internal.observers.ConsumerSingleObserver.onError(ConsumerSingleObserver.java:46)
        at io.reactivex.internal.operators.single.SingleDoOnSubscribe$DoOnSubscribeSingleObserver.onError(SingleDoOnSubscribe.java:86)
        at io.reactivex.internal.operators.single.SingleDoOnSuccess$DoOnSuccess.onError(SingleDoOnSuccess.java:65)
        at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:79)
        at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:124)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6946)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
     Caused by: java.io.FileNotFoundException: /external/images/media/78207: open failed: ENOENT (No such file or directory)
        at libcore.io.IoBridge.open(IoBridge.java:456)
        at java.io.FileInputStream.<init>(FileInputStream.java:76)
        at okio.Okio.source(Okio.java:168)
        at okhttp3.RequestBody$3.writeTo(RequestBody.java:170)
        at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.java:173)
        at okhttp3.MultipartBody.writeTo(MultipartBody.java:114)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:69)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:43)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
        at com.asenadev.sana.model.remote.ApiServiceProvider.lambda$getRetrofitAuth$0(ApiServiceProvider.java:25)
        at com.asenadev.sana.model.remote.-$$Lambda$ApiServiceProvider$u0pzdrwgIKkHYeztqBLPTvSP6wQ.intercept(lambda)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229)
        at okhttp3.RealCall.execute(RealCall.java:81)
        at retrofit2.OkHttpCall.execute(OkHttpCall.java:204)
        at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:46)
        at io.reactivex.Observable.subscribe(Observable.java:12284)
        at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:35)
        at io.reactivex.Observable.subscribe(Observable.java:12284)
    	at io.re

نمیدونم مشکل کار کجاست واقعا

فایل پیوست

علی باقری
علی باقری

1 آذر 99

2
حذف شده

سلام و احترام

 

کدتونو که بررسی کردم مشکلی نداشت و طبق اروری که فرستادید  :

۱-مطمين شوید که به درستی عمل Subscibe رو انجام دااده باشید.

subscribeOn(shedulers.io())
    .observeOn(AndroidSchedulers.mainThread())

۲-احتمالا مشکل از انتخاب فایل(عکس) هست که به درستی صورت نمیگیرد که برای این مورد دو منبع معرفی میکنم که عالی توضیح دادن(متن + ویدیو) که البته اولی از Rx استفاده نشده :

طبق اروری که فرستادید یعنی:

java.io.FileNotFoundException: /external/images/media/۷۸۲۰۷: open failed: ENOENT (No such file or directory)

 

اموزش :

https://futurestud.io/tutorials/retrofit-۲-how-to-upload-a-dynamic-amount-of-files-to-server

آموزش طبق Rx (مقاله):

https://medium.com/@PaulinaSadowska/display-progress-of-multipart-request-with-retrofit-and-rxjava-۲۳a۴a۷۷۹e۶ba

فایل پیوست

پوریا شفیعی

توسط

پوریا شفیعی

1 آذر 99

حذف شده
مرسی پوریا جان جواب رو خودمم ارسال کردم اینجا اگه کسی مشکل منو داشت حل بشه کارش
علی باقری

3 آذر 99

1
حذف شده

سلام مشکل از جای دیگه ای بود که فیکسش کردم اون فایلی که از گالری کاربر میگرفت تا برای ارسال کردن استفاده بشه یه uri به صورت خروجی داشت که صرفا برای android os قابل استفاده بود و باید ادرس دقیقش توی external storage رو پیدا کرد .

که به صورت زیر رفعش کردم

 

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch (requestCode) {
            case CAMERA_REQUEST_CODE:

            case GALLERY_REQUEST_CODE:
                if (resultCode == Activity.RESULT_OK) {
                    Uri imagePath = imageReturnedIntent.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor=getActivity().getContentResolver().query(imagePath,filePathColumn,null,null,null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();

                    imageFile = new File(picturePath);
                    Picasso.get()
                            .load(imageFile)
                            .into(profilePicIv);

                }
                break;
        }
    }

 

[تاپیک بسته شد ]

فایل پیوست

علی باقری

توسط

علی باقری

3 آذر 99