2014-08-29 6 views
0

REST WCF 웹 서비스가있는 Android가 있습니다. POST 형식이 복잡한 유형 매개 변수 유형을 호출 할 때 매개 변수는 null입니다. WCF 매개 변수 POST가 Android를 사용하는 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; 
    } 

안드로이드 : 나는 웹 서비스를 디버깅 갔지 방법의 속성 설정이
private void preencherPropriedadesConexao(HttpURLConnection httpURLConnection) { 
     IApplication application = (IApplication) Factory.getInstance().getInstanceFor(IApplication.class); 
     httpURLConnection.setRequestProperty("Content-Type", "application/json"); 
     httpURLConnection.setRequestProperty("Accept", "application/json"); 
     } 
    } 

를 POST 매개 변수 "pedido"의 값은 null입니다. 왜?

감사합니다.

답변

0

이 문제를 해결합니다 !!

Json에서 Gson으로 변환 할 때 시스템에서 문자열을 식별하기 때문에 발생합니다. . 이것에 대한

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

:!

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

감사

따라서, 시스템이 파라미터가 null의 원인, 이전과 JSON 때까지 "포함

는이 문제를 해결하기 위해, 나는이 줄을 변경

관련 문제