无法使用Android客户端发布到WCF服务

无法使用Android客户端发布到WCF服务,android,wcf,json,service,client,Android,Wcf,Json,Service,Client,我有一个运行的自托管WCF web服务和一个Android客户端应用程序。我能够以json格式从web服务获取或检索数据,但是我无法向服务器发布或发送任何数据 以下是WCF服务的代码: [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/SetValue", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json

我有一个运行的自托管WCF web服务和一个Android客户端应用程序。我能够以json格式从web服务获取或检索数据,但是我无法向服务器发布或发送任何数据

以下是WCF服务的代码:

 [OperationContract]
 [WebInvoke(Method = "POST",
 UriTemplate = "/SetValue",
 RequestFormat = WebMessageFormat.Json,
 ResponseFormat = WebMessageFormat.Json,
 BodyStyle = WebMessageBodyStyle.Wrapped)]
 public string SetValue(TestClass someValue)
 {
     return someValue.Something.ToString();
 }

[DataContract]
public class TestClass
{
    [DataMember(Name = "something")]
    public int Something
    {
        get;
        set;
    }
}
以下是来自Android客户端的代码:

 HttpClient httpClient = new DefaultHttpClient();
 HttpPost request = new HttpPost("http://xxx.xxx.x.x:8000/SetValue");
 List<NameValuePair> params = new ArrayList<NameValuePair>(1);
 params.add(new BasicNameValuePair("something", "12345"));
 request.setEntity(new UrlEncodedFormEntity(params));
 HttpResponse response = httpClient.execute(request);
我只有一个app.config,它只有:

 <?xml version="1.0"?>
 <configuration>
 <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
差不多就是这样。我对WCF非常陌生,不知道这个“服务器日志”会在哪里,我不知道如何对其进行故障排除或调试?(我试过Fiddler2,但它似乎没有从Android客户端检测到任何东西。)

[编辑]

我也试过了

 JSONObject json = new JSONObject(); 
 json.put("something", "12345"); 
 StringEntity entity = new StringEntity(json.toString()); 
 entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
 entity.setContentType( new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));  
 request.setEntity(entity); 
这也会导致错误


我还注意到,如果我将“SetValue”更改为返回一个常量,例如“abcd”,而不是someValue.Something.ToString(),那么一切都可以正常工作?

您的客户端代码正在发送一个html表单后期格式化的负载,但您的服务器需要一个json负载,您需要的客户端类似于

HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost("http://xxx.xxx.x.x:8000/SetValue");
StringEntity e = new StringEntity("{ \"something\":12345 }", "UTF-8");
request.setEntity(e);
request.setHeader("content-type", "application/json");
HttpResponse response = httpClient.execute(request);

您的客户机代码正在发送一个html表单后格式化负载,但您的服务器需要一个json负载,您需要的客户机类似于

HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost("http://xxx.xxx.x.x:8000/SetValue");
StringEntity e = new StringEntity("{ \"something\":12345 }", "UTF-8");
request.setEntity(e);
request.setHeader("content-type", "application/json");
HttpResponse response = httpClient.execute(request);

我也有同样的问题,我通过删除

BodyStyle = WebMessageBodyStyle.WrappedRequest
从我的WCF方法头

 [WebInvoke(Method = "POST", UriTemplate = "mymethod", RequestFormat=WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
                ResponseFormat=WebMessageFormat.Json)]
更改为

 [WebInvoke(Method = "POST", UriTemplate = "mymethod", RequestFormat=WebMessageFormat.Json,
                ResponseFormat=WebMessageFormat.Json)]

我也有同样的问题,我通过删除

BodyStyle = WebMessageBodyStyle.WrappedRequest
从我的WCF方法头

 [WebInvoke(Method = "POST", UriTemplate = "mymethod", RequestFormat=WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
                ResponseFormat=WebMessageFormat.Json)]
更改为

 [WebInvoke(Method = "POST", UriTemplate = "mymethod", RequestFormat=WebMessageFormat.Json,
                ResponseFormat=WebMessageFormat.Json)]
超级电池那个正解!


超级电池那个正解!

根据我的发现,Android将JSON字符串转换为字节数组流,然后发布它。下面是我自己的代码作为示例

HttpPost httpPost = new HttpPost(URL_Base + uri);
httpPost.setEntity(new StringEntity(sJSONOut));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpEntity oHttpEntity = new DefaultHttpClient().execute(httpPost).getEntity();
在eclipse中,当在倒数第二行设置断点并检查httpPost对象属性时,我发现StringEntity的值不是字节数组[123.43,234……尽管我已经确认字符串sJSONOut的格式正确

这里的另一个答案建议从WCF方法头中删除BodyStyle=WebMessageBodyStyle.WrappedRequest。这为我提供了将该行从WrappedRequest更改为Bare所需的线索,而这正是最终起作用的原因


BodyStyle=WebMessageBodyStyle.Bare

根据我的发现,Android将JSON字符串转换为字节数组流,然后发布。以下是我自己的代码作为示例

HttpPost httpPost = new HttpPost(URL_Base + uri);
httpPost.setEntity(new StringEntity(sJSONOut));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpEntity oHttpEntity = new DefaultHttpClient().execute(httpPost).getEntity();
在eclipse中,当在倒数第二行设置断点并检查httpPost对象属性时,我发现StringEntity的值不是字节数组[123.43,234……尽管我已经确认字符串sJSONOut的格式正确

这里的另一个答案建议从WCF方法头中删除BodyStyle=WebMessageBodyStyle.WrappedRequest。这为我提供了将该行从WrappedRequest更改为Bare所需的线索,而这正是最终起作用的原因


BodyStyle=WebMessageBodyStyle.Bare

谢谢你的建议,不幸的是我仍然收到了错误。我也尝试过:
JSONObject json=new JSONObject();json.put(“something”,“12345”);StringEntity=new StringEntity(json.toString());entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,“application/json”);entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,“application/json”);request.setEntity(entity);
,这也会导致错误。我还注意到,如果我更改“SetValue”以返回常量(“abcd”)而不是someValue.Something.ToString(),然后一切都正常了?然后听起来好像请求使代码正常,但代码抛出了异常。好的,你是对的。我在SetValue(TestClass someValue)中添加了以下“if语句”:
if(someValue==null)返回“someValue为null”;
并返回此语句。看起来客户端指定的值“12345”由于某种原因未被传递:我认为您需要了解dataContract如何映射到json。如果我将SetValue定义更改为:SetValue(string something),然后返回此输入字符串,我会看到“12345”正如预期的那样。至少它现在可以用于简单类型,尽管我在复杂类型(即传递类对象)方面没有像原始帖子中那样成功。感谢您的建议,不幸的是我仍然收到错误。我也尝试过:
JSONObject json=new JSONObject();json.put(“something”,“12345”);StringEntity entity=new StringEntity(json.toString());entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,“application/json”);entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,“application/json”);request.setEntity(entity)
这也会导致错误。我还注意到,如果我将“SetValue”更改为返回一个常量(“abcd”),而不是someValue.Something.ToString(),然后一切都正常了?然后听起来好像请求使代码正常,但代码抛出了异常。好的,你是对的。我在SetValue(TestClass someValue)中添加了以下“if语句”:
if(someValue==null)返回“someValue为null”;
并返回此语句。看起来客户端指定的值“12345”由于某种原因未被传递:我认为您需要了解dataContract如何映射到json。如果我将SetValue定义更改为:SetValue(string something),然后返回此输入字符串,我会看到“12345”正如预期的那样。至少现在它可以用于简单类型,尽管我不能成功地用于复杂类型(即传递类对象)正如在原来的帖子中一样。试试这个我希望这是非常有帮助的。试试这个我希望这是非常有帮助的。请总是尝试写至少一个小的解释你的答案。请总是尝试写至少一个小的解释你的答案。