1

我有一个 .NET 4.5 WCF Web 服务,它的界面中有以下代码:

[OperationContract]
[WebInvoke(Method = "POST",
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "UpdCategorieArticoli",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json)]
Task<JsonGenericResponse> UpdCategorieArticoli(string jsonData);

并且是这样实现的(在综合系统中):

public async Task<JsonGenericResponse> UpdCategorieArticoli(string jsonData)
{
    JsonGenericResponse objWmJson = new JsonGenericResponse();
    objWmJson.IsInError = "true";
    try
    {
        DataIfCloudEspresso objData = new DataIfCloudEspresso();
        string xmlData = <some transformation from json>;
        int intUpdatedCount = await objData.UpdCategorieArticoli(xmlData );
        if (intUpdatedCount == 1)
        {
            objWmJson.IsInError = "false";
            objWmJson.Value = intUpdatedCount.ToString();
        }
        return objWmJson;
    }
}

JsonGenericResponse 是一个类:

public class JsonGenericResponse
{
    string _Value;
    public string Value
    {
        get
        {
            return _Value;
        }
        set
        {
            _Value = value;
        }
    }
    .... Other properties
}

数据层中被调用的方法:

public async Task<int> UpdCategorieArticoli(string xmlData)
{
    try
    {
        using (SqlConnection objConn = new SqlConnection(base.ConnectionString))
        {
            using (SqlCommand cmdADO = new SqlCommand("spUpd_CategorieArticoli", objConn))
            {
                cmdADO.Parameters.Add(new System.Data.SqlClient.SqlParameter("@XMLDoc", SqlDbType.Text));
                cmdADO.Parameters["@XMLDoc"].Value = xmlData;
                cmdADO.Parameters.Add(new System.Data.SqlClient.SqlParameter("@RETURN_VALUE", SqlDbType.Int));
                cmdADO.Parameters["@RETURN_VALUE"].Direction = ParameterDirection.ReturnValue;
                //
                cmdADO.CommandType = CommandType.StoredProcedure;
                //
                await objConn.OpenAsync();
                await cmdADO.ExecuteNonQueryAsync();
                return (int)cmdADO.Parameters["@RETURN_VALUE"].Value;
            }
        }
    }
}

此代码已从 jQuery 成功调用并且它正在工作,调用为

$.ajax({
    type: "POST",
    url: "WEspresso.svc/UpdCategorieArticoli",
    data: strData,
    async: true,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    complete: saveCategorieArticoli_onComplete
});

在回调中,我可以看到我的 json 字符串,基于 JsonGenericResponse 类,正如预期的那样。

当我尝试从 Android 调用此 Web 服务时,问题就来了。如果我执行合同为

[OperationContract]
[WebInvoke(Method = "POST",
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "UpdCategorieArticoliSync",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json)]
JsonGenericResponse UpdCategorieArticoliSync(string jsonData);

如果我使用断点从 Visual Studio 2012 启动调试站点,则实现中的一些假代码我可以看到该方法已到达,而不是如果我尝试调用返回 Task<> 的方法,则 Visual Studio 2012 中的断点是未从我的 Android 代码中获取:

public class wsWcfUpdCateg extends AsyncTask<String, Integer, String>{
    public final String SOAP_ACTION = "http://tempuri.org/UpdCategorieArticoli";
    public  final String SOAP_ADDRESS = "http://192.168.0.1/CloudTest/wespresso.svc/UpdCategorieArticoli";
public Context maincontext; 
HttpResponse objResponse = null;

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);             
}

@Override
protected String doInBackground(String... arg0) {
    StringBuilder objString = new StringBuilder();
    try
    {
        HttpClient objHttpClient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(objHttpClient.getParams(), 10000);
        JSONObject json = new JSONObject();
        HttpPost objPost = new HttpPost(SOAP_ADDRESS);
        //
        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("jsonData", arg0[0].toString()));
        for (NameValuePair p : params){
            json.put(p.getName(), p.getValue());
        }        
        StringEntity se = new StringEntity(json.toString());
        objPost.setEntity(se);
        objPost.setHeader("Accept", "application/json");
        objPost.setHeader("Content-type", "application/json");  
        objResponse = objHttpClient.execute(objPost);              
        StatusLine objStatusLine = objResponse.getStatusLine();
        Integer intStatus = objStatusLine.getStatusCode();              
        //
        return objString.toString() ;    
    }   
}

}

在上面的代码中, intStatus 是 400 ,如果我只是将 UpdCategorieArticoli 更改为 UpdCategorieArticoliSync 而不是我得到 200 (并且到达.NET代码中的断点)。如何实现 Android 代码,以便它可以与返回 Task 的 WCF 方法一起使用,并使用 JSON 作为响应格式?我应该使用Android的“同步版本”,否则没有希望?

4

0 回答 0