1

我有一个 RESTful WCF 服务,它的一种方法使用对象作为参数

[WebInvoke(UriTemplate = "save", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat= WebMessageFormat.Xml), OperationContract]
        public SampleItem Create(SampleItem instance)
        {
            return new SampleItem() { Id = 1, StringValue = "saved" };
            // TODO: Add the new instance of SampleItem to the collection
            //throw new NotImplementedException();
        }

我正在尝试从我的 eclipse android 项目中调用此方法。我正在使用这些代码行

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post=new HttpPost("http://10.0.2.2:2768/Service1.svc/save");
ArrayList<NameValuePair> nvp= new ArrayList<NameValuePair>();

nvp.add(new BasicNameValuePair("Id", "1"));
nvp.add(new BasicNameValuePair("StringValue", "yolo"));

post.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse httpResponse = httpClient.execute(post);
HttpEntity httpEntity = httpResponse.getEntity();
String xml = EntityUtils.toString(httpEntity);

每次我Method not allowed.在服务方法返回的 XML 中收到此错误时。

我尝试从浏览器调用它,但在那里遇到了同样的错误。

请告诉我我做错了什么以及我能做些什么。

提前感谢任何可以提供帮助的人。

注意:其他不使用对象作为参数的方法工作正常。

编辑:成功尝试了 Fiddler2。但又停滞了。

我尝试SampleItem Create(SampleItem instance)使用 url 调用该方法http://localhost:2768/Service1.svc/save并且它有效。该方法以 XML 格式返回对象。

在提琴手中,我将请求正文添加为 <SampleItem xmlns="http://schemas.datacontract.org/2004/07/WcfRestService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Id>1</Id><StringValue>saved</StringValue></SampleItem>

但问题是我找不到任何方法将此 xml 字符串添加到HttpPostHttpRequest作为requestbody eclipse android 项目。

注意:将 xml 字符串作为HeaderUrlEncodedFormEntity传递不起作用。

4

3 回答 3

2

最后我成功地将一个json对象发送到我的WCF 服务这是我的代码

URI uri = new URI("http://esimsol.com/droidservice/pigeonlibrary.service1.svc/save");

JSONObject jo1 = new JSONObject();
jo1.put("Id", "4");
jo1.put("StringValue", "yollo");

HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();
conn.setRequestProperty("Content-Type","application/json; charset=utf-8");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("User-Agent", "Pigeon");
conn.setChunkedStreamingMode(0);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.connect();

DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(jo1.toString().getBytes());
out.flush();

int code = conn.getResponseCode();
String message = conn.getResponseMessage();

InputStream in = conn.getInputStream();
StringBuffer sb = new StringBuffer();
String reply;

try {
int chr;
while ((chr = in.read()) != -1) {
sb.append((char) chr);
}
reply = sb.toString();
} finally {
in.close();
}

SampleItem SI = new SampleItem();
SI=new Gson().fromJson(reply, SampleItem.class);

Toast.makeText(getApplicationContext(), SI.getStringValue(),Toast.LENGTH_LONG).show();

conn.disconnect();

感谢StackOverFlow。我不得不结合一些代码片段来实现这一点。

于 2012-06-26T04:46:02.183 回答
0

首先,您应该让 Web 服务方法在浏览器中工作——我建议使用 Fiddler2——它更容易用您的对象构造请求主体,并且在发布帖子时也可以设置请求标头。它将向您显示响应,因此应该有助于调试。至于您的代码,我正在对 WCF 服务进行 POST 而不是

post.setEntity(new UrlEncodedFormEntity(nvp));

我只是在做:

HttpPost request = new HttpPost(url);

// Add headers.
for(NameValuePair h : headers)
{
     request.addHeader(h.getName(), h.getValue());
}

(我正在使用 JSONObjects,并且我的 WebInvoke 参数中有 RequestFormat = WebMessageFormat.Json。

此外,请检查您在 url 中使用正确的 UriTemplate 名称,因为它们区分大小写。

于 2012-06-02T13:10:06.647 回答
0

要调用该 WCF 服务,您必须构建有效的 SOAP 请求并发布它。最好在 Android 上使用一些 SOAP 协议栈 - 例如kSoap2

这是使用 kSoap2 调用 WCF 服务的示例。只需在您的项目中添加 KSOAP2 库。关于我们如何在 Android 项目中添加 KSOAP2,请参阅这篇文章

于 2012-06-02T14:11:41.600 回答