私は Android クライアントから使用して安らかな WCF を持っています。Android からサービスを呼び出すと、ここで問題がわかりません。「Request Error (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();