android - 无法从 Android rest_upload_sideload_error 上传到 wordpress
问题描述
我正在尝试使用以下路径通过 WP REST API 上传文件:/wp-json/wp/v2/media
POSTMAN 的工作正常,但是当我尝试从 android 应用程序上传时,出现此错误:
“代码”:“rest_upload_invalid_disposition”
这是上传方法:
private void SendDetail(final String token) {
uploadMultipart();
final ProgressDialog loading = new ProgressDialog(UploadPhoto.this);
loading.setMessage("Please Wait...");
loading.show();
loading.setCanceledOnTouchOutside(false);
DefaultRetryPolicy mRetryPolicy = new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
StringRequest stringRequest = new StringRequest(Request.Method.POST, Const.UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Tsto", "onResponse:1 "+response);
try {
loading.dismiss();
Log.d("JSON", response);
JSONObject eventObject = new JSONObject(response);
String error_status = eventObject.getString("error");
if (error_status.equals("true")) {
String error_msg = eventObject.getString("msg");
ContextThemeWrapper ctw = new ContextThemeWrapper( UploadPhoto.this, R.style.Theme_AppCompat);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
alertDialogBuilder.setTitle("Vendor Detail");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage(error_msg);
alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
alertDialogBuilder.show();
} else {
String error_msg = eventObject.getString("msg");
ContextThemeWrapper ctw = new ContextThemeWrapper( UploadPhoto.this, R.style.Theme_AppCompat_Dialog_Alert);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
alertDialogBuilder.setTitle("Registration");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage(error_msg);
// alertDialogBuilder.setIcon(R.drawable.doubletick);
alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent=new Intent(UploadPhoto.this,LoginActivity.class);
startActivity(intent);
finish();
}
});
alertDialogBuilder.show();
}
}catch(Exception e){
Log.d("Tag", e.getMessage());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
....*** Error Handling here ...
alertDialogBuilder.show();
}
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = new HashMap<String,String>();
map.put(KEY_User_Document1,Document_img1);
Log.d("Network", map.toString());
return map;
}
@Override //Send Header
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<>();
//params.put("api_call_header", "header_value");
params.put("Authorization", "Bearer " + token);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
stringRequest.setRetryPolicy(mRetryPolicy);
requestQueue.add(stringRequest);
}
这是我使用的 uploadMultipart 方法
public void uploadMultipart() {
//getting name for the image
// String name = editText.getText().toString().trim();
//getting the actual path of the image
String path = getPath(filePath);
//Uploading code
try {
String uploadId = UUID.randomUUID().toString();
// Random Name for avatars
final String random = GeneratePassword.randomString(7);
//Creating a multi part request
new MultipartUploadRequest(this, uploadId, Const.UPLOAD_URL)
.addFileToUpload(path, "image") //Adding file
.addParameter("name", random) //Adding text parameter to the request
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload(); //Starting the upload
} catch (Exception exc) {
Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
}
}
所以我在这里做错了什么
解决方案
推荐阅读
- javascript - 将自定义 javascript 文件放在 GWT JSinterop java 程序中的什么位置?
- c++ - 有效计算大型 C++ 数组中的平均值
- reactjs - 如何在反应播放器中停止视频?
- python - 如何为特定功能的 python 调试设置 VsCode launch.json?
- r - 尝试从 R 中的 GLM 预测新数据时出现持续错误
- python - 每个类别的分数计数
- compiler-errors - 我收到有关 omnet 模拟中 udp 消息的序列化程序的错误。如何解决?
- linux - 不尊重清单中定义的用户
- java - 如何从 FireStore 读取 ArrayList?
- javascript - 如何在数组中添加人物?