我有注册页面,在这个页面中我必须使用Volley库将人物图像和人员详细信息一起发送到我的服务器,问题是我正在寻找如何做但仍未找到。有人可以帮助我吗
JSONObject request = new JSONObject();
try {
request.put("username", "ramu");
request.put("password", "12345");
request.put("firstname", "rk");
request.put("lastname", "ram");
//Along with this parameters i have to send person image also
} catch (JSONException e) {
e.printStackTrace();
}
String url = "MYURL";
VolleyBackgroundCode.getJsonRequest(MainActivity.this, url, request.toString());
public static void getJsonRequest(Context _Context, String url, final String body) {
RequestQueue queue = Volley.newRequestQueue(_Context.getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
return;
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
return headers;
}
@Override
public byte[] getBody() {
return body.toString().getBytes();
}
@Override
public String getBodyContentType() {
return "application/json";
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
return super.parseNetworkResponse(response);
}
};
request.setRetryPolicy(new DefaultRetryPolicy(2000, 3, 1.5f));
queue.add(request);
}
}
答案 0 :(得分:0)
使用多部分请求
HashMap<String, String> params = new HashMap<String, String>();
String url = "YOUR POST URL";
String image_path = "your local image path";
params.put("your_extra_params", "value");
MultipartRequest multipartRequest =
new MultipartRequest(url, params, image_path, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e(TAG, "Success Response: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error.networkResponse != null) {
Log.e(TAG, "Error Response code: " +
error.networkResponse.statusCode);
}
});
requestQueue.add(multipartRequest);