2012-06-28 2 views
0

웹 서비스에 일부 데이터를 게시해야하는 앱을 쓰고 있습니다. 이렇게하려면 that tutorial을 사용했습니다.게시 안드로이드, 웹 서비스에 연결할 수 없습니다.

"get"메서드는 작동하지만 포스트는 작동하지 않습니다.

Webservice를 코드 : 같은 웹 서비스에 의해 예상되는 어떤

[OperationContract] 
[WebInvoke(Method = "POST", 
ResponseFormat = WebMessageFormat.Json, 
RequestFormat = WebMessageFormat.Json, 
UriTemplate = "picture")] 
public void UploadPicture(RReport report) 
{ 
    if(report == null) 
     throw new WebFaultException<string>("Bad parameter", HttpStatusCode.BadRequest); 

    //other stuff 
} 

가 보일 것입니다 :

{ "PictureBase64"다음

는 예를 들어 요청 JSON 몸이다 "문자열 콘텐츠 "}

그리고 지금은 안드로이드 부분 :

지금
//Localhost URI 
String url = "http://10.0.2.2:51136/API/picture"; 
RestClient client = new RestClient(url); 
try { 
    Report r = new Report("ok"); 

    //Convert in json 
    String report = new Gson().toJson(r); 
    //What shows the log => {"PictureBase64":"ok"} 
    Log.i("Json", report); 
    //Add the param 
    client.AddParam("report", report); 

    //set the header 
    client.AddHeader("Content-Type","application/json"); 
    client.Execute(RequestMethod.POST); 
} catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

String response = client.getResponse(); 
Log.i("Response", response); 

그리고 안드로이드 부분에서 오류 :

?<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>Request Error</title> 
     <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style> 
    </head> 
    <body> 
     <div id="content"> 
     <p class="heading1">Request Error</p> 
     <p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="http://10.0.2.2:51136/API/help">service help page</a> for constructing valid requests to the service.</p> 
     </div> 
    </body> 
</html> 

편집 :

DefaultHttpClient httpClient = new DefaultHttpClient(); 
HttpPost postRequest = new HttpPost(myWebservice); 

StringEntity input = new StringEntity(myJson); 
input.setContentType("application/json"); 
postRequest.setEntity(input); 

HttpResponse response = httpClient.execute(postRequest); 

답변

1

당신이 JSON에 콘텐츠 형식을 설정하는 : 새로운 방법은 내 웹 서비스를 호출 하지만 JSON을 게시하지 않으면 키 값 쌍을 게시하고 있습니다. 여기서 값은 문자열 (JSON 일 수 있지만 그와는 무관합니다)입니다. 웹 서비스에서 JSON 본문 또는 양식 데이터를 원하십니까?

JSON 본문이 아닌 매개 변수를 게시하려는 경우 mime 유형을 application/x-www-form-urlencoded으로 설정하고 매개 변수를 인코딩하는 URL인지 확인하십시오 (아마도 restlet이 해당 작업을 수행할지 모르겠습니다).

반면에

, 당신은 실제로 단지이 문제가 해결되지 않는 가정도,

StringRepresentation sr = new StringRepresentation(myJsonString); 
Representation rep = client.post(); 

을하는 JSON 본체를 게시 그대로 MIME 타입을두고 같은 작업을 수행하려는 경우 서버에서 상태 코드를 확인하는 것이 좋습니다. 404? 400? 403? 사용중인 웹 서비스가 올바른 코드를 반환한다고 가정하면 근본 원인을 찾는 데 매우 도움이 될 수 있습니다.

+0

오류는 400입니다. 헤더를 변경하면 도움이되지 않습니다. – David

+0

은 요청을 올바르게 공식화하지 않음을 의미합니다. 웹 서비스의 정의를 살펴보면 요청 형식이 JSON임을 나타냅니다. 내가 말했듯이 JSON을 전달하지 않고 양식 데이터 매개 변수를 전달합니다. 이 경우 오류 400이 예상됩니다. –

+0

Thx, 나는 당신의 조언으로 내 코드를 변경하고 현재 작동 중입니다. (내 편집 참조) – David

관련 문제