我正在尝试使用截击将图像上传到服务器,我遵循了一些教程,但就我而言,我需要在发布请求的正文中传递多部分数据。
private void uploadBitmap(final Bitmap bitmap) throws JSONException {
//our custom volley request
String URL = "https://<---------->/me/avatar";
JSONObject jsonBody = new JSONObject();
jsonBody.put("avatar", new VolleyMultipartRequest.DataPart( "index.png", getFileDataFromDrawable(bitmap)));
final String requestBody = jsonBody.toString();
VolleyMultipartRequest volleyMultipartRequest = new VolleyMultipartRequest(Request.Method.POST, URL,
new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
loading.setVisibility(View.GONE);
Toast.makeText(ProfileSettings.this, "Image uploaded successfully", Toast.LENGTH_SHORT).show();
try {
JSONObject obj = new JSONObject(new String(response.data));
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json; charset=UTF-8");
params.put("Authorization", "Bearer " + jsonToken);
return params;
}
我从教程中获得了这段代码,但他们给出了 500 错误,所以我猜这可能是因为我需要在请求正文中传递“avatar”:“index.png”而不是这种方式。
慕尼黑的夜晚无繁华
慕婉清6462132
相关分类