簡體   English   中英

如何使用改造將圖像發送到服務器

[英]How do I send image to the server using retrofit

我正在嘗試使用改造、多部分/表單數據將圖像發送到服務器。 但我得到一個錯誤。

我試過通過郵遞員發送圖片,沒問題。

@Multipart
@POST("requests/11006/history")
fun sendMessageWithImg(
    @Header("X-Device-UDID") deviceUDID: String = "",
    @Header("Authorization") token: String,
    @Part image: MultipartBody.Part
): Call<ResponseBody>


private fun upload(fileUri: Uri) {
    val tokenStorage = TokenStorage(this)
    val token = tokenStorage.getToken()
    val originalFile = FileUtils.getFile(this, fileUri)

    val filePart = RequestBody.create(
        MediaType.parse(contentResolver.getType(fileUri)),
        originalFile)

    val file: MultipartBody.Part = MultipartBody.Part.createFormData("image[]", originalFile.name, filePart)

    val client = ApiService.create()

    val call: Call<ResponseBody> = client
        .sendMessageWithImg(
            token = "Bearer $token",
            image = file)

    call.enqueue(object : Callback<ResponseBody>{

        override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
            print(call)
        }

        override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
            print(response)
        }

    })
}

錯誤是 - 無法從 /fec0::9c04連接到.30::681c:587(端口 443)。 (端口 38630)10000 毫秒后

試試這個代碼

@Multipart
@POST("user/updateprofile")
Observable<ResponseBody> updateProfile(@Part("user_id") RequestBody id,
                                   @Part("full_name") RequestBody fullName,
                                   @Part MultipartBody.Part image,
                                   @Part("other") RequestBody other);



 //pass it like this
File file = new File("/storage/emulated/0/Download/Corrections 6.jpg");
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);

// add another part within the multipart request
 RequestBody fullName = 
    RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name");

  service.updateProfile(id, fullName, body, other);

以下是可能發生的情況:

1- 您的手機無法連接到服務器,因為它未連接到互聯網或與您的服務器位於同一網絡。 確保連接正常工作。

2- 手機可以連接到服務器,但是圖像太大,超過了 Http 客戶端中使用的默認超時 10 秒。 如果您使用 OkHttp 作為 Retrofit 的 Http 客戶端,那么您可以將此超時值更改為更高的值( 查看此鏈接了解更多信息)

OkHttpClient okHttpClient = new OkHttpClient.Builder()  
        .connectTimeout(1, TimeUnit.MINUTES)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(15, TimeUnit.SECONDS)
        .build();

Retrofit.Builder builder = new Retrofit.Builder()  
        .baseUrl("http://10.0.2.2:3000/")
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create());

3- 問題也可能出在服務器端。 如果在服務器上設置的圖像大小限制低於圖像的實際大小,這可能是它失敗的原因。

 //add perimissiom in mainfests
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    //interface    
        @Multipart
        @POST("requests/11006/history")
        fun sendMessageWithImg(
                           @Part("X-Device-UDID") RequestBody deviceUDID,
                            @Part("Authorization") RequestBody token,
           @Part List<MultipartBody.Part> file1
        ): Call<ResponseBody>

     //below code placed in your Activity java class inside of oncreate
    chooseanimage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                        // intent.putExtra(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                        Log.i("cameraa", "01");
                    }
                    startActivityForResult(Intent.createChooser(intent, "select"), 0);
                }
            });

            if (Build.VERSION.SDK_INT < 23) {
                //Do not need to check the permission
            } else {
                RequestMultiplePermission();
            }



      upload..setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                uploadalbum(fileUris);
});



           //below code placed in your Activity java class outside of oncreate
                  private void RequestMultiplePermission() {

                    // Creating String Array with Permissions.
                    ActivityCompat.requestPermissions(Create_Project.this, new String[]
                            {
                                    CAMERA,
                                    WRITE_EXTERNAL_STORAGE
                            }, RequestPermissionCode);
                }
                 @NonNull
                    private RequestBody createPartFromString(String descriptionString) {
                        return RequestBody.create(
                                okhttp3.MultipartBody.FORM, descriptionString);
                    }

                    @NonNull
                    private MultipartBody.Part prepareFilePart(String partName, Uri fileUri) {
                        // https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
                        // use the FileUtils to get the actual file by uri
                        //   File file = new File(strImgaepath);
                        File file = FileUtils.getFile(this, fileUri);
                        // create RequestBody instance from file
                        RequestBody requestFile =
                                RequestBody.create(
                                        MediaType.parse(getContentResolver().getType(fileUri)),
                                        file
                                );

                        // MultipartBody.Part is used to send also the actual file name
                        return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
                    }


       @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            if (requestCode == 0 && data != null) {


                ClipData clipData = data.getClipData();

                if (data.getClipData() != null) {
                    int count = data.getClipData().getItemCount();
                    int currentItem = 0;
                    while (currentItem < count) {
                        Uri imageUri = data.getClipData().getItemAt(currentItem).getUri();
                        //do something with the image (save it to some directory or whatever you need to do with it here)
                        currentItem = currentItem + 1;
                        Log.d("Uri Selected", imageUri.toString());
                        try {
                            // Get the file path from the URI
                            String path = FileUtils.getPath(this, imageUri);
                            Log.d("Multiple File Selected", path);

                            fileUris.add(imageUri);
                            //  MyAdapter mAdapter = new MyAdapter(Create_Project.this, arrayList);
                            // listView.setAdapter(mAdapter);

                        } catch (Exception e) {
                            //   Log.e(TAG, "File select error", e);
                        }
                    }
                } else if (data.getData() != null) {
                    //do something with the image (save it to some directory or whatever you need to do with it here)
                    final Uri uri = data.getData();
                    Log.i("sara", "Uri = " + uri.toString());
                    try {
                        // Get the file path from the URI
                        final String path = FileUtils.getPath(this, uri);
                        Log.d("Single File Selected", path);

                        fileUris.add(uri);

                        if (fileUris != null) {
                            setimage.setVisibility(View.VISIBLE);
                        }

                    } catch (Exception e) {
                        Log.e("sara", "File select error", e);
                    }
                }
            }





        }


    //upolad image
     private void uploadalbum(ArrayList<Uri> fileUris) {
            pDialog = new ProgressDialog(this);
            pDialog.show();
            pDialog.setMessage("Loading");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            ApiGetPost service = ApiConstant.getMainUrl(getApplicationContext()).create(ApiGetPost.class);


            List<MultipartBody.Part> parts = new ArrayList<>();
            for (int i = 0; i < fileUris.size(); i++) {

                parts.add(prepareFilePart("file_upload[]", fileUris.get(i)));
                Log.i("sara", String.valueOf(fileUris.get(i)));
            }


            updateMyProfile = service.upload(createPartFromString(strtitle),

                    createPartFromString(""),
                    createPartFromString(""), parts);

            updateMyProfile.enqueue(new Callback<ModelLogin>() {
                @Override
                public void onResponse(Call<ModelLogin> call, Response<ModelLogin> response) {
                    pDialog.dismiss();
                    ModelLogin user = response.body();
                    String sa = String.valueOf(user.getStatus());
                    String msg = user.getMessage();

                    if (sa.equals("true")) {
                        Toast.makeText(getApplicationContext(), "" + msg, Toast.LENGTH_SHORT).show();


                    }

                }

                @Override
                public void onFailure(Call<ModelLogin> call, Throwable t) {

                }
            });
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM