尝试使用改装多部件在api服务器上传图像时,我收到以下103错误消息,api接收api令牌,“_ method”:“PUT”和图像url作为参数,响应时收到完整的
JSON但使用上一个或默认图像链接,当前所选图像不上传,其他一切都是可选的,任何帮助将不胜感激,代码列在下面谢谢.
主要的方法是POST,但“_method”:“PUT”参数允许你上传图像,没有_method它成为POST消息和其他所有参数而不是强制,请检查图像.
com.sun.jdi.InternalException: Unexpected JDWP Error: 103
接口:
public interface UserSignUpClient { @POST("account") Call<AuthenticationAccountCreationResponse> createAccount(@Body UserSignUp userSignUp); @Multipart @PUT("account") Call<UserInfo> postImage(@Header("Authorization") String headerValue,@PartMap Map<String,String> map,File> imageMap,@Part MultipartBody.Part image); @FormUrlEncoded @POST("account") Call<UserInfo> updateUser(@Header("Authorization") String headerValue,@FieldMap Map<String,String> map); }
改造生成器类:
public class RestClient { private UserSignInClient userSignInClient; private UserSignUpClient userSignUpClient; private UserInfoClient userInfoClient; public RestClient(String baseUrlLink){ Gson gson = new GsonBuilder() .setLenient() .create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrlLink) .addConverterFactory(GsonConverterFactory.create(gson)).build(); if (baseUrlLink.equals(CONSTS.BASE_URL_ADDRESS_TOKEN)){ userSignInClient = retrofit.create(UserSignInClient.class); } else if (baseUrlLink.equals(CONSTS.BASE_URL_ADDRESS_ACCOUNT_CREATION)) { userSignUpClient = retrofit.create(UserSignUpClient.class); } else if (baseUrlLink.equals(CONSTS.BASE_URL_ADDRESS_USER_INFO)){ userInfoClient = retrofit.create(UserInfoClient.class); } } public UserSignInClient getUserSignInClient() { return userSignInClient; } public UserSignUpClient getUserSignUpClient() { return userSignUpClient; } public UserInfoClient getUserInfoClient() { return userInfoClient; } }
主类:
userImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { galleryIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); galleryIntent.setType("*/*"); startActivityForResult(galleryIntent,RESULT_LOAD_IMG); } }); @Override public void onActivityResult(int requestCode,int resultCode,Intent data) { super.onActivityResult(requestCode,resultCode,data); if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) { Uri selectedImageUri = data.getData(); String filePath = FileUtils.getPath(this,selectedImageUri); file = new File(filePath); image_name = file.getName(); if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)) { } else { ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); } } } } @Override public void onRequestPermissionsResult(int requestCode,String permissions[],int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { StringBuilder stringBuilder = new StringBuilder(AUTHORIZATION); sharedPreferences = this.getSharedPreferences(getResources().getString(R.string.token),0); stringBuilder.append(sharedPreferences.getString(getResources().getString(R.string.token),"")); RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"),file); MultipartBody.Part body = MultipartBody.Part.createFormData("image",file.getName(),requestBody); UserUpdate userUpdate = new UserUpdate(file); methodMap.put("_method","PUT"); imageMap.put("image",file); Call<UserInfo> uploadImage = new RestClient(CONSTS.BASE_URL_ADDRESS_ACCOUNT_CREATION).getUserSignUpClient() .postImage(stringBuilder.toString(),methodMap,imageMap,body); uploadImage.enqueue(new Callback<UserInfo>() { @Override public void onResponse(Call<UserInfo> call,Response<UserInfo> response) { if (response.isSuccessful()) { Picasso.with(DetailActivity.this).load("http://ec2-35-161-195-128.us-west-2.compute.amazonaws.com/" + response.body().getImage()).into(userImage); } } @Override public void onFailure(Call<UserInfo> call,Throwable t) { Toast.makeText(DetailActivity.this,t.getMessage(),Toast.LENGTH_SHORT).show(); } }); } else { // permission denied,boo! Disable the // functionality that depends on this permission. } return; } // other 'case' lines to check for other // permissions this app might request } } } return; } // other 'case' lines to check for other // permissions this app might request } }
postman api image = https://i.imgur.com/AoCFZI6.png
@H_403_22@解决方法
我认为您的问题是您忘记在改装对象中添加呼叫适配器.在您的RestClient类中,您必须输入以下内容:
Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrlLink) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build();
您还需要在build.gradle中添加此依赖项:
compile "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
我希望它有所帮助.
干杯.
@H_403_22@ @H_403_22@ 原文链接:https://www.f2er.com/android/129535.html