2013-02-26 31 views
5

Tôi gặp sự cố khi đăng một JSONArray các giá trị cho Dịch vụ WCF của mình. Khi tôi đăng dữ liệu từ Fiddler hoặc .Net Test Client nó hoạt động tốt. Mỗi khi tôi cố gắng đăng bài từ ứng dụng Android của mình, tôi nhận được Yêu cầu LỗiĐăng một JSONArray lên Dịch vụ WCF từ android

Đây là dữ liệu JSON mà tôi gửi tới Dịch vụ WCF của mình từ ứng dụng Android. Tôi đã thử dữ liệu này chính xác từ Fiddler và nó hoạt động

[{"date":"2013-02-22 15:30:374:021","id":"1","description":"test","name":"test"}, 
"date":"2013-02-25 11:56:926:020","id":"2","description":"ghy","name":"fhh"}, 
"date":"2013-02-25 11:56:248:026","id":"3","description":"ghfm","name":"run"}] 

mã Android My

public JSONObject makeHttpPost(String url, String method, JSONArray params) { 
    try { 

     if (method == "POST") { 

      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      httpPost.setHeader("Accept", "application/json"); 
      httpPost.setHeader("Content-Type", 
      "application/json; charset=utf-8"); 
      StringEntity se = new StringEntity(params.toString(),"UTF-8"); 

      se.setContentType("application/json;charset=UTF-8"); 
      httpPost.setEntity(se); 

      Log.e("Gerhard", params.toString()); 
      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 



     } 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

My WCF Service

[OperationContract] 
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "updateOrderAddress")] 
    String UpdateOrderAddress(Stream JSONdataStream); 

public String UpdateOrderAddress(Stream JSONdataStream) 
    { 
     try 
     { 
      // Read in our Stream into a string... 
      StreamReader reader = new StreamReader(JSONdataStream); 
      string JSONdata = reader.ReadToEnd(); 

      // ..then convert the string into a single "wsOrder" record. 

      if (JSONdata == null) 
      { 
       // Error: Couldn't deserialize our JSON string into a "wsOrder" object. 
       return "null"; 
      } 



      return JSONdata;  // Success ! 
     } 
     catch (Exception e) 
     { 
      return e.ToString(); 
     } 
    } 

Các lỗi tôi nhận được

02-26 14:00:56.185: E/Gerhard(31064):  <p>The server encountered an error processing the request. The exception message is 'Incoming message for operation 'UpdateOrderAddress' (contract 'IService1' with namespace 'http://tempuri.org/') contains an unrecognized http body format value 'Json'. The expected body format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is: </p> 

Tôi đã gọi nhiều yêu cầu GET ts từ ứng dụng android cho cùng một dịch vụ WCF và nó hoạt động tốt, nhưng bây giờ tôi cần gửi một mảng dữ liệu đến dịch vụ wcf. Xin hãy giúp me.Thanks trước

+0

Bạn nhận bất kỳ lỗi nào? Câu trả lời là gì? – Rajesh

+0

Xin lỗi bạn vừa thêm lỗi vào bài đăng – razeth01

+0

Bạn có thể thử không có 'se.setContentType (" application/json; charset = UTF-8 ");'? – Rajesh

Trả lời

6

loại bỏ

httpPost.setHeader("Accept", "application/json");     
httpPost.setHeader("Content-Type", "application/json; charset=utf-8"); 

từ mã ur

+0

Điều này cũng đã giải quyết được sự cố mà tôi đã cố gắng gửi tin nhắn thử nghiệm từ người chơi. Tôi rất thích một lời giải thích ... –

+0

nó giúp rất nhiều !! thx – anevil

Các vấn đề liên quan