2013-10-08 67 views
1

以下代碼給出錯誤的請求錯誤,代碼中的任何解決方案或錯誤。上傳文件Android org.apache.http.entity.mime.MultipartEntity:錯誤的請求錯誤

MultipartEntity entityPost = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 

entityPost.addPart("data_1", new StringBody(String.valueOf(feedbackId), Charset.forName("UTF-8")));   
entityPost.addPart("file_1", new FileBody(__file)); 

HttpPost httppost = new HttpPost("http://www.example.com/webservice.asmx/method"); 
httppost.setEntity(entityPost); 
httppost.setHeader("Content-Type", "multipart/form-data"); 

HttpResponse __response = HttpManager.httpClient().execute(httppost); 

的WebService:

public String method() {  
    try { 
     System.Web.HttpContext postContext = System.Web.HttpContext.Current; 

     string data = postContext.Request.Form["data_1"].ToString(); 

     System.Web.HttpFileCollection files = postContext.Request.Files;   
     System.Web.HttpPostedFile = files[0]; 

     //etc etc 

    } catch (Exception ex) { 
     // 
    } 
} 

錯誤:

org.apache.http.client.HttpResponseException: Bad Request 

預先感謝您

+0

和哪裏是Web服務代碼? –

+1

正確詳細說明您的問題 – 2013-10-08 15:48:55

+0

錯誤的請求:400是客戶端請求錯誤 在它進入webservice之前發生異常。 該web服務代碼已經使用正常的html multipart/form-data進行了測試 如果在webservice上有任何錯誤,它會給出500+錯誤 – sawbeanraz

回答

0

正如薩蒂亞Komatineni,戴夫·麥克萊恩,賽義德Y.在書臨的Android 3.0提到哈希米。 外部庫中加入:

共享IO:HTTP // commons.apache.org/IO/

Mime4j:HTTP // james.apache.org/mime4j/

HttpMime:http // hc.apache.org/downloads.cgi(HttpClient內部)

它可以正常工作,與之前創建的web服務相同。

File file = new File(filePath);  
InputStream is = new FileInputStream(file); 

HttpClient httpClient = new DefaultHttpClient(); 
HttpPost postRequest = new HttpPost("http://mysomewebserver.com/services/doSomething.do"); 


byte[] data = IOUtils.toByteArray(is); 

InputStreamBody isb = new InputStreamBody(new 
ByteArrayInputStream(data), "filename"); 
StringBody sb1 = new StringBody("some text goes here"); 
StringBody sb2 = new StringBody("some text goes here too"); 
MultipartEntity multipartContent = new MultipartEntity(); 
multipartContent.addPart("uploadedFile", isb); 
multipartContent.addPart("one", sb1); 
multipartContent.addPart("two", sb2); 

postRequest.setEntity(multipartContent); 
HttpResponse response =httpClient.execute(postRequest); 
response.getEntity().getContent().close(); 

但有警告說MultipartEntity被取消了。我對此不太確定。