我正在尝试通过AsyncHttpClient post方法将文件发送到服务器。我需要将文件附加到正文中。我正在尝试通过params这样做,但是它没有用。 每当我遇到错误400错误的请求时。
try {
File f = new File(tryGetUnencryptedFile(fileItems[0]).getPath());
params.put("api_key", "---------------------------------------");
params.put("token", "----------------------------------------");
params.put("file_name", f.getName());
params.put("file", f);
System.out.println(params);
client.post( url, params, new AsyncHttpResponseHandler() {
ProgressDialog pd;
@Override
public void onStart() {
String uploadingMessage = getResources().getString(R.string.send);
pd = new ProgressDialog(MainActivity.this);
pd.setTitle(R.string.waiting_info);
pd.setMessage(uploadingMessage);
pd.setIndeterminate(false);
pd.show();
System.out.println(params);
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println(statusCode);
String status = getResources().getString(R.string.ok);
Toast.makeText(MainActivity.this, status, Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
System.out.println(statusCode);
String status = getResources().getString(R.string.general_error);
Toast.makeText(MainActivity.this, status, Toast.LENGTH_SHORT).show();
}
@Override
public void onFinish() {
pd.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}