웹 서비스에 일부 데이터를 게시해야하는 앱을 쓰고 있습니다. 이렇게하려면 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);
오류는 400입니다. 헤더를 변경하면 도움이되지 않습니다. – David
은 요청을 올바르게 공식화하지 않음을 의미합니다. 웹 서비스의 정의를 살펴보면 요청 형식이 JSON임을 나타냅니다. 내가 말했듯이 JSON을 전달하지 않고 양식 데이터 매개 변수를 전달합니다. 이 경우 오류 400이 예상됩니다. –
Thx, 나는 당신의 조언으로 내 코드를 변경하고 현재 작동 중입니다. (내 편집 참조) – David