使用Android,WCF参数POST为null

时间:2014-08-29 12:44:14

标签: c# android web-services wcf httpurlconnection

我有一个带有REST WCF Web服务的Android应用程序。当我调用方法类型时POST有一个复杂的类型参数,参数为null。

我的界面服务:

[OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json, UriTemplate = "/gravar")]
    void Gravar(PedidoMobileContrato pedido);

服务:

public void Gravar(PedidoMobileContrato pedido)
        {
            var test = "Test";
        }

合同Pedido:

[DataContract]
    public class PedidoMobileContrato
    {
        /// <summary>
        /// Construtor sem parâmetro
        /// </summary>
        public PedidoMobileContrato()
        {
        }   


    /// <summary>
    /// O valor.
    /// </summary>
    [DataMember]
    public double Valor { get; set; }        
}

Android应用程序

public void gravar(Pedido pedido) {            
        JSONObject json = new JSONObject();
        Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                return !f.getName().toUpperCase().equals("VALOR");                    
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        }).create();
        String jsonConvertido = gson.toJson(pedido);

        try {
            json.put("pedido", jsonConvertido);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        this.executarMetodoPost(this.enderecoURL + "gravar", json).then(new Callback<String>() {
            @Override
            public void onSuccess(String s) {
                String teste = s;
                teste = "";
            }

            @Override
            public void onFailure(Exception e) {
                e.printStackTrace();
            }
        });
    }

Andoid:执行POST的方法:

protected IPromiseResult<String> executarMetodoPost(String enderecoURL, JSONObject parametros) {
        IPromiseResult<String> promessa = new Promise<String>();

        try {
            URL url = new URL(enderecoURL);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            preencherPropriedadesConexao(httpURLConnection);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.connect();

            if (parametros != null) {
                DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());
                out.write(parametros.toString().getBytes());
                out.flush();
            }

            int codigoResposta = httpURLConnection.getResponseCode();

            if (codigoResposta != 200) {
                promessa.setResult(httpURLConnection.getResponseMessage());
                httpURLConnection.disconnect();
            } else {
                promessa.setResult(String.valueOf(codigoResposta));
            }

            httpURLConnection.disconnect();
        } catch (Exception e) {
            promessa.setError(e);
        }
        return promessa;
    }

Android:方法设置属性POST

private void preencherPropriedadesConexao(HttpURLConnection httpURLConnection) {
        IApplication application = (IApplication) Factory.getInstance().getInstanceFor(IApplication.class);
        httpURLConnection.setRequestProperty("Content-Type", "application/json");
        httpURLConnection.setRequestProperty("Accept", "application/json");
        }
    }

当我调试Web服务时,参数&#34; pedido&#34;的值一片空白。为什么呢?

谢谢!

1 个答案:

答案 0 :(得分:0)

我解决了这个问题!!

这是因为在从Json转换为Gson时系统会识别它是如何串的。因此,系统包括“在json之前和之前,导致参数为空。

为了解决这个问题,我更改了这一行:

json.put("\"pedido\"", jsonConvertido);

为此:

json.put("pedido", new JSONObject(new Gson().toJson(pedidoMobileContrato)));

谢谢!