我需要使用Android中的凌空库将2个文件发送到服务器。 邮递员有一个很好的例子: 我需要在android中重现此POST调用。 请暂时看一下我的代码(不起作用):
JsonObjectRequest sr = new JsonObjectRequest(Request.Method.POST, URL, new JSONObject(params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("Response", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<>();
params.put("Urine", "test");
return params;
}
@Override
public Map<String, String> getHeaders() {
Map<String,String> params = new HashMap<>();
params.put("Authorization", "token");
return params;
}
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=utf-8";
}
@Override
public byte[] getBody() {
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
byte[] data = response.data;
String message = new String(data);
Log.i("parseNetworkResponse", String.valueOf(message));
return super.parseNetworkResponse(response);
}
};
如何使用Volley库实现此目的?谢谢。
答案 0 :(得分:3)