【发布时间】:2018-11-26 07:42:29
【问题描述】:
我在使用改造 2 上传图片时遇到了一些问题。我有一个 api 来上传三种图像,例如(个人资料图像、横幅图像、其他图像)。我需要传递三个参数(user_id、type(profile/banner/other)、media(file))...我不明白该怎么做...
这是我的界面...
@Multipart
@POST("media/upload_media")
Call<ServerRespose> upload(
@Part MultipartBody.Part file ,
@Query("user_id") int user_id ,
@Query("type") String type
);
这是我想要做的事情......
private void uploadFile(String path, Uri fileUri, final int type) {
// create upload service client
uid = DatabaseUtil.getInstance().getUser().getData().getID();
String username = SharedPreferenceUtil.getStringValue(this, Constants.USERNAME);
String password = SharedPreferenceUtil.getStringValue(this, Constants.PASSWORD);
if (!username.isEmpty() && !password.isEmpty()) {
Api service =
RetrofitUtil.createProviderAPIV2(username, password);
//
try {
// use the FileUtils to get the actual file by uri
showProgressDialog("Uploading");
File file = new File(path);
RequestBody requestFile =
RequestBody.create(
MediaType.parse(getContentResolver().getType(fileUri)),
file
);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("file", file.getName(), requestFile);
// finally, execute the request
Call<ServerRespose> call = service.upload(body , uid , "profile_image");
call.enqueue(new Callback<ServerRespose>() {
@Override
public void onResponse(Call<ServerRespose> call,
Response<ServerRespose> response) {
hideProgressDialog();
Log.v("Upload", "success");
ServerRespose item = response.body();
try {
if (item != null) {
// item.setSuccess(true);
if (type == SELECT_PROFILE_PIC) {
profileImageRecyclerViewAdapter.addNewItem(item);
profileImageRecyclerViewAdapter.notifyDataSetChanged();
} else {
bannerImageRecyclerViewAdapter.addNewItem(item);
bannerImageRecyclerViewAdapter.notifyDataSetChanged();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ServerRespose> call, Throwable t) {
AppUtils.showDialog(Profile_Activity.this, "There is some Error", null);
Log.e("Upload error:", t.getMessage());
}
});
} catch (Exception e) {
e.printStackTrace();
}
} else {
showDialogSignedUp("Session Expired Please Login Again...", null);
}
}
注意:我的代码不能正常工作,只是选择图像并显示上传,它也没有返回任何类型的响应......请任何人帮助我需要在很短的时间内完成这项工作的正确代码。 检查这里的参数...
function save_image($request)
{
if(!empty($request['user_id'])){
$user_identity = $request['user_id'];
$submitted_file = $_FILES['media'];
$uploaded_image = wp_handle_upload( $submitted_file, array( 'test_form' => false ) );
$type = $request[ 'type' ];
//return $submitted_file;
if ( !empty( $submitted_file )) {
$file_name = basename( $submitted_file[ 'name' ] );
$file_type = wp_check_filetype( $uploaded_image[ 'file' ] );
// Prepare an array of post data for the attachment.
$attachment_details = array(
'guid' => $uploaded_image[ 'url' ],
'post_mime_type' => $file_type[ 'type' ],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_name ) ),
'post_content' => '',
'post_status' => 'inherit'
);
【问题讨论】:
-
确保您的密钥与服务器请求密钥相同
-
先签到邮递员
-
@Query与@GET方法一起使用。你应该使用@Field -
哪个键? @AndroidTeam
-
它在邮递员@AndroidTeam 中正常工作
标签: java android retrofit retrofit2