AsyncHttpClient send multipart/form-data

不想你离开。 提交于 2019-12-11 17:21:25

问题


I use a AsyncHttpClient to send a request. I have to send to server multipart/form-data it should be like :

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--

I did this but it doesn't work as expected. I want to send a photo and a json to the server. I use the same parameters from API Request in the android client the request doesn't work correctly.

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);
        }
    });
}

When run the code, I receive a status code 415, I am not sure what went wrong.

来源:https://stackoverflow.com/questions/47754023/asynchttpclient-send-multipart-form-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!