2012-07-24 2 views
12

안전한 안심할 수있는 웹 서비스를 통해 노래 (mp3/wav) 파일과 일부 데이터를 전송한다고 가정합니다. MultipartEntity를 사용하여 HttpPost 요청을하고 있습니다. 메시지 상태 보고서 : 잘못된 요청 클라이언트가 보낸 요청의 구문이 잘못되었습니다 (잘못된 요청) 잘못된 요청 유형 - 나는 HttpClient를 통해 실행할 때, 서버는이 오류안드로이드 오류 : MultipartEntity, 클라이언트가 보낸 요청이 구문 상 올바르지 않음

에게 HTTP 상태 400 응답합니다.

그러나 웹 인터페이스에서 호출하면 서비스가 잘 수행됩니다.

의 코드

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

      reqEntity.addPart("email", new StringBody("[email protected]")); 
      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()); 
     } 

나는 또한 MultipartEntity에 대한 아파치 mime4j, HttpClient를, httpcore 및 httpmime 단지를 포함 도와주세요.

이것은 서비스의 HTML 페이지 스냅입니다. enter image description here

+0

인증이 제대로 작동합니까? – Mark

+0

예 ..... 그렇습니다 – Azhar

+0

웹 서비스에도 매핑 URL을 게시 할 수 있습니까? URL 요청이 매핑 한 것과 다를 수 있습니다. – tolgap

답변

5

다음과 같이 setURI 메서드를 제거하고 HttpPost 개체를 만들 때 URL을 전달하십시오. 이것은 나를 위해 일했습니다 (more here).

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

    reqEntity.addPart("email", new StringBody("[email protected]")); 
    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()); 
} 
+0

을 그냥 받아들입니다. – Azhar

2

그나마 다른 다른 combinations.There을 시도하여 시간을 낭비 당신이 getting.Ex 있습니다 요청 및 응답을 추적 할 수있는 HTTP에 사용할 일부 HTTP 요청 도구입니다. HTTP Analyzer 다운로드 평가판

작업중인 웹 인터페이스에서 복사 요청 및 응답 을 호출 한 다음 프로그램에서와 동일하게이 도구는 요청 및 응답 데이터를 캡처 할 수 있습니다.

작동 여부와 비 작동 요구를 비교하면 헤더 문제인지 인증 관련 문제인지 여부를 분명히 판단 할 수 있습니다.

관련 문제