0

我有一个从 Android 客户端使用的宁静 WCF。当我从android调用服务时,我不明白这里的问题,我得到'请求错误(400)'。这是服务代码

<system.serviceModel>
<services>
  <service name="JooyaSilver.MyCOMWebServices.MyCOMServices" behaviorConfiguration="MyServiceBehavior">
    <endpoint address=""
              binding="webHttpBinding"
              contract="JooyaSilver.MyCOMWebServices.IMyCOMServices"
              behaviorConfiguration="httpBehavior"></endpoint>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="httpBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

[ServiceContract()]
public interface IMyCOMServices
{
    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "TestConnection",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    string TestConnection();

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "SubmitTrackPoint",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    int SubmitTrackPoint(ServiceData sdata);        

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "GetMyTasks",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    ServiceData GetMyTasks(ServiceData sdata);        

}   
[DataContract()]
public class ServiceData
{
    [DataMember] public string SData;
    [DataMember] public bool IsActionSucceed;       
}

这是安卓代码

public static HttpPost getHttpPost(String serviceUril, String methodName) {

    String uri = uriBaseAddress + serviceUril + "/" + methodName;
    HttpPost request = new HttpPost(uri);
    request.setHeader("Accept", "application/json");
    request.setHeader("Content-type", "application/json");
    return request;
}

public static StringEntity getStringEntity(String message) throws UnsupportedEncodingException {
    StringEntity msg = new StringEntity(message);
    msg.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    msg.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    return msg;
}

public static JSONStringer getCompleteJsonStringer(String sendingData) {
    JSONStringer requestMsg = null;
    try {
        requestMsg = new JSONStringer().object().key("sdata").object()
                .key("SData").value(sendingData)
                .key("IsActionSucceed").value(true)
                .endObject()
                .endObject();
        Logger.d(requestMsg.toString());
    } catch (JSONException e) {
        Logger.e(e.getMessage());
    }
    return requestMsg;
}
HttpPost request = ServicesHelper.getHttpPost(GeneralServices_URI, "SubmitTrackPoint");
HttpClient httpClient = new DefaultHttpClient();

StringBuilder sb = new StringBuilder();
// some code here for data
JSONStringer requestMsg = ServicesHelper.getCompleteJsonStringer(sb.toString());
StringEntity msg2 = ServicesHelper.getStringEntity(requestMsg.toString());
request.setEntity(msg2);
HttpResponse response = httpClient.execute(request);
int scr = response.getStatusLine().getStatusCode();
4

1 回答 1

0

我已经解决了这个问题,在启用服务跟踪后,我发现我的服务的记录器在它的静态构造函数中抛出了一个错误。解决问题后一切正常

于 2013-01-23T17:36:26.687 回答