我使用AsyncHttpClient发送请求。 我必须发送到服务器multipart / form-data,它应该是:
POST http://localhost:8080/SC_TerminalAssist/proxy/photos/note HTTP/1.1
Accept-Encoding: gzip,deflate
Authorization: SCToken b970d85e-278b1163-1592-4e9c-9255-459022a585e7
Content-Type: multipart/form-data; boundary="----=_Part_4_1087532223.1512997169142"
MIME-Version: 1.0
Content-Length: 869
Host: localhost:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
------=_Part_4_1087532223.1512997169142
Content-Type: application/json; name=login_param.json
Content-Transfer-Encoding: binary
Content-Disposition: form-data; name="metadata"; filename="login_param.json"
{
"company": "elte gps",
"user": "franek",
"secure_password": "C4CA4238A0B923820DCC509A6F75849B",
"secure_device_id": "C4CA4238A0B923820DCC509A6F75849B"
}
------=_Part_4_1087532223.1512997169142
Content-Type: text/xml; charset=Cp1250; name=login_param.xml
Content-Transfer-Encoding: binary
Content-Disposition: form-data; name="photos"; filename="login_param.xml"
<login_param>
<company>elte gps</company>
<user>franek</user>
<secure_password>C4CA4238A0B923820DCC509A6F75849B</secure_password>
<secure_device_id>C4CA4238A0B923820DCC509A6F75849B</secure_device_id>
</login_param>
------=_Part_4_1087532223.1512997169142--
我这样做但是它没有按预期工作。我想将照片和json发送到服务器。我在Android客户端使用API请求中的相同参数请求无法正常工作。
private void uploadFileExecute(File file, String token) {
JSONObject jsonObject = new JSONObject();
SharedPreferences sp ;
sp = getSharedPreferences("pfref", Activity.MODE_PRIVATE);
String pass = sp.getString("pass", "");
String firm = sp.getString("firm", "");
String login = sp.getString("login", "");
try {
jsonObject.put("company", firm);
jsonObject.put("user", login);
jsonObject.put("secure_password", HashUtil.md5(pass));
jsonObject.put("secure_device_id", HashUtil.md5(DeviceUtil.getDeviceId(PhotoActivity.this)));
} catch (JSONException e) {
e.printStackTrace();
}
RequestParams params = new RequestParams();
BasicHeader[] headers = new BasicHeader[]{new BasicHeader("Authorization", "SCToken " + token)};
try {
params.put("photos", file);
params.put("filename",jsonObject);
} catch (FileNotFoundException e) {
}
client.post(PhotoActivity.this, RestClient.getAbsoluteUrl("photos/note"), headers, params, "application/json", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.d("sokces", "uploadFile response: " + statusCode);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.d("sokces nie", "uploadFile ERROR! " + statusCode);
}
});
}
运行代码时,我收到状态代码415,我不知道出了什么问题。