Android错误:MultipartEntity,客户端发送的请求在语法上不正确

Azh*_*har 12 java android httpclient http-post multipartentity

我想通过安全的restful web服务发送歌曲(mp3/wav)文件和一些数据.我正在使用MultipartEntity来发出HttpPost请求.但是当我通过HttpClient执行它时,服务器会回复此错误

HTTP状态400 - 错误请求类型:状态报告消息:错误请求客户端发送的请求在语法上不正确(错误请求).

但是,如果我们从Web界面调用它,那么该服务的效果非常好.请帮忙

它的代码

HttpClient httpclient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost();
        try {
            MultipartEntity reqEntity = new MultipartEntity();

            reqEntity.addPart("email", new StringBody("test@testmail.com"));
            reqEntity.addPart("password", new StringBody("123"));
            reqEntity.addPart("title", new StringBody("My new song"));
            reqEntity.addPart("musicData", new FileBody(new File(FilePath))); 

            // FIlePath is path to file and contains correct file location

            postRequest.setEntity(reqEntity);

            postRequest.setURI(new URI(ServiceURL));
            HttpResponse response = httpclient.execute(postRequest);

        } catch (URISyntaxException e) {
            Log.e("URISyntaxException", e.toString());
        } 
Run Code Online (Sandbox Code Playgroud)

我还为MultipartEntity提供了apache-mime4j,httpclient,httpcore和httpmime jar.

这是服务的HTML页面捕捉. 在此输入图像描述

Kyl*_*egg 5

尝试删除setURI方法并在创建HttpPost对象时传入URL,如下所示.这对我有用(更多这里).

HttpClient httpclient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(ServiceURL);
try {
    MultipartEntity reqEntity = new MultipartEntity();

    reqEntity.addPart("email", new StringBody("test@testmail.com"));
    reqEntity.addPart("password", new StringBody("123"));
    reqEntity.addPart("title", new StringBody("My new song"));
    reqEntity.addPart("musicData", new FileBody(new File(FilePath)));     
    postRequest.setEntity(reqEntity);

    HttpResponse response = httpclient.execute(postRequest);

} catch (URISyntaxException e) {
    Log.e("URISyntaxException", e.toString());
} 
Run Code Online (Sandbox Code Playgroud)