Android的错误:MultipartEntity,请求客户端发送的是语法不正确 [英] Android error : MultipartEntity , request sent by the client was syntactically incorrect

查看:184
本文介绍了Android的错误:MultipartEntity,请求客户端发送的是语法不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过安全RESTful Web服务发送歌曲(MP3 / WAV)文件和一些数据。我使用MultipartEntity使HttpPost请求。但是当我通过HttpClient的执行它时,服务器回复此错误

I am suppose to send song (mp3/wav) file and some data through secure restful web service. I am using MultipartEntity to make HttpPost request. but When I execute it through HttpClient, the server replies this error

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

HTTP Status 400 - Bad Request type: Status report message : Bad Request The request sent by the client was syntactically incorrect (Bad Request).

但服务做得非常好,如果我们从它的Web界面调用它。请大家帮忙

But the service is doing very well if we call it from its Web interface. please help

它在code

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

我还包括Apache的mime4j,HttpClient的,的HttpCore和httpmime罐子MultipartEntity。

I also included apache-mime4j, httpclient, httpcore and httpmime jars for MultipartEntity.

这是对服务的HTML网页快照。

This is HTML page snap for the Service.

推荐答案

尝试取出setURI方法并传入当您创建HttpPost对象中的URL,如下所示。这为我工作(更这里)。

Try removing the setURI method and passing the URL in when you create your HttpPost object, as follows. This worked for me (more here).

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

这篇关于Android的错误:MultipartEntity,请求客户端发送的是语法不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆