WCF参数POST在使用Android时为null

本文关键字:Android 时为 null 参数 POST WCF | 更新日期: 2023-09-27 18:20:08

我有一个带有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; }        
}

安卓应用程序

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服务时,参数"pedido"的值为null。为什么?

谢谢!

WCF参数POST在使用Android时为null

我解决了这个问题!!

之所以出现这种情况,是因为在从Json转换为Gson时,系统会如何识别字符串。因此,系统包含"之前和之后json,导致参数为空。

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

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

为此:

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

谢谢!