Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从java调用对象作为参数的RESTful WCFservice方法_Android_Http Post_Wcf Rest_Webinvoke - Fatal编程技术网

Android 从java调用对象作为参数的RESTful WCFservice方法

Android 从java调用对象作为参数的RESTful WCFservice方法,android,http-post,wcf-rest,webinvoke,Android,Http Post,Wcf Rest,Webinvoke,我有一个RESTful WCF服务,它的一个方法使用对象作为参数 [WebInvoke(UriTemplate = "save", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat= WebMessageFormat.Xml), OperationContract] public SampleItem Create(SampleItem instance) {

我有一个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();
        }
我正试图从我的EclipseAndroid项目中调用此方法。我正在使用这些代码行

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);
DefaultHttpClient-httpClient=newdefaulthttpclient();
HttpPost=新的HttpPost(“http://10.0.2.2:2768/Service1.svc/save");
ArrayList nvp=新的ArrayList();
nvp.add(新的BasicNameValuePair(“Id”,“1”));
nvp.add(新的BasicNameValuePair(“StringValue”、“yolo”);
post.setEntity(新的UrlEncodedFormEntity(nvp));
HttpResponse HttpResponse=httpClient.execute(post);
HttpEntity HttpEntity=httpResponse.getEntity();
字符串xml=EntityUtils.toString(httpEntity);
每次我在服务方法返回的XML中得到此错误
方法不允许。

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

请告诉我我做错了什么,我能做些什么

提前感谢任何能提供帮助的人

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

编辑:成功地尝试了小提琴2。但又停滞不前了

我已尝试使用url调用方法
SampleItem Create(SampleItem实例)
http://localhost:2768/Service1.svc/save,它可以工作。该方法以XML格式返回对象

在fiddler中,我将请求主体添加为
1已保存

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


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

首先,您应该从浏览器中获得Web服务方法-我建议使用Fiddler2-使用对象构造请求主体以及在发布时设置请求头更容易。它将向您显示响应,因此应该有助于调试。 至于您的代码,我正在向WCF服务发布一篇文章,而不是

post.setEntity(new UrlEncodedFormEntity(nvp));
我只是在做:

HttpPost request = new HttpPost(url);

// Add headers.
for(NameValuePair h : headers)
{
     request.addHeader(h.getName(), h.getValue());
}
(我使用的是JSONObject,我的WebInvoke参数中有RequestFormat=WebMessageFormat.Json


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

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

下面是使用kSoap2调用WCF服务的示例。
只需在您的项目中添加KSOAP2 lib。有关如何在Android项目中添加KSOAP2,请参见

最后,我成功地将一个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();
多亏了堆栈溢出
我必须结合许多代码片段来实现这一点。

这真的很有帮助,谢谢,但我想使用XML将对象从WCF服务传递到WCF服务。您是否可以帮助我,或者建议使用JSON或XML。我尝试了fiddler,但仍然卡住了。请查看我的编辑和共享。嗨,我实际上尝试了ksoap2.b但这对我没有任何好处。所以我切换到了REST。请看我的编辑和分享。