【问题标题】:Posting android, unable to contact the web service发布android,无法联系网络服务
【发布时间】:2012-06-30 04:01:32
【问题描述】:

我正在编写一个必须在网络服务上发布一些数据的应用程序。 为此,我使用了that tutorial

“get”方法有效,但 post 无效。

网络服务代码:

[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
}

webservice 的预期应该是这样的:

以下是请求Json正文示例:

{ "PictureBase64":"字符串内容" }

现在是 android 部分:

//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);

现在是android部分的错误:

?<?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);

【问题讨论】:

    标签: android wcf wcf-rest


    【解决方案1】:

    您将内容类型设置为 JSON,但您没有发布 JSON,您发布的是键值对,其中值是字符串(恰好是 JSON,但这无关紧要)。您的 Web 服务需要 JSON 正文还是表单数据?

    假设您要发布参数,而不是 JSON 正文,请将您的 mime 类型设置为:application/x-www-form-urlencoded,并确保您对参数进行 URL 编码(也许 restlet 会为您执行此操作,我不确定) .

    另一方面,如果您实际上只是想发布 JSON 正文,请保留 MIME 类型,然后执行类似的操作,

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

    另外,假设这不能解决您的问题,最好知道来自服务器的状态代码。 404? 400? 403?假设您正在使用的 Web 服务返回正确的代码,这可能对找到根本原因非常有帮助。

    【讨论】:

    • 这意味着您没有正确地制定请求。查看 Web 服务的定义,它声明请求格式是 JSON。就像我说的,你不是在传递 JSON,而是在传递表单数据参数。在这种情况下,预计会出现错误 400。
    • 谢谢,我根据您的建议更改了我的代码,现在它正在工作。 (见我的编辑)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多