2011-01-29 69 views
0

我對RESTFull WCF服務非常陌生,甚至更新的來自Android應用程序調用它們。這裏是我的WCF服務:jsonarray文本必須以{['位於{我的返回值}的字符1開頭

[ServiceContract] 
    public interface IPeople 
    { 
     [OperationContract] 
     void DoWork(); 

     [WebGet(UriTemplate = "/GetPeople", 
      BodyStyle = WebMessageBodyStyle.WrappedRequest, 
      ResponseFormat = WebMessageFormat.Json, 
      RequestFormat = WebMessageFormat.Json)] 
     [OperationContract] 
     string GetPeople(); 
    } 

接口的實現:

公共字符串GetPeople(){ PeoplesEntities QE =新PeoplesEntities();

   var result = from q in qe.tPeople 
          select q; 


      int count = result.Count(); 
      int index = new Random().Next(count); 

      tPeople people = result.OrderBy(a=>a.ID).Skip(index).FirstOrDefault(); 

      // result.First().ToString(); 


      return people.FirstName + " - " + people.LastName; 
    } 

,這是如何我消費它通過機器人服務:

嘗試{

 HttpGet request = new HttpGet(SERVICE_URI + "/GetPeople"); 
     request.setHeader("Accept", "application/json"); 
     request.setHeader("Content-type", "application/json"); 

     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpResponse response = httpClient.execute(request); 

     HttpEntity responseEntity = response.getEntity(); 

     // Read response data into buffer 
     char[] buffer = new char[(int)responseEntity.getContentLength()]; 
     InputStream stream = responseEntity.getContent(); 
     InputStreamReader reader = new InputStreamReader(stream); 
     reader.read(buffer); 
     stream.close(); 

     JSONArray plates = new JSONArray(new String(buffer)); 
       return new String(buffer); 



    } catch (Exception e) { 
     e.printStackTrace(); 
     return e.toString(); 
    } 
} 

我得到的例外是什麼在主題中提到。奇怪的是,我們期望的價值會在例外情況下返回。我不知道爲什麼期待方括號。如果您有任何疑問,請聯繫我們,我們會盡快爲您解答。任何幫助將不勝感激。謝謝。

+0

JSON和http身份驗證讓我頭痛在android。但保留,你會通過它。如果您不確定JSOn結構,我建議您閱讀它並學習如何手寫JSON結構,以便了解C#代碼的功能。 – JonWillis 2011-01-29 22:53:53

回答

2

您試圖從不包含有效JSON數組語法的字符串創建JSONArray。可以使用[item1, item2, item3....]形式的字符串創建JSONArray,但您只需返回字符串中的單個項目:FirstName LastName

它後面的行只是返回緩衝區,所以JSONArray調用是毫無意義的。你根本不需要JSONArray調用,因爲你沒有處理JSON數據。只要刪除該行。

相關問題