2011-05-21 156 views
0

我想用POST/PUT發送JSON我不知道它是否一樣。 這是json。 {「author」:「mehdi」,「email」:「[email protected]」,「message」:「Hello2」} 這是我的wadl。用POST發送JSON到Restful web服務

<resource path="/messages/"><method id="get" name="GET"><request><param name="start" style="query" type="xs:int" default="0"/><param name="max" style="query" type="xs:int" default="10"/><param name="expandLevel" style="query" type="xs:int" default="1"/><param name="query" style="query" type="xs:string" default="SELECT e FROM Message e"/></request><response><representation mediaType="application/xml"/><representation mediaType="application/json"/></response></method><method id="post" name="POST"><request><representation mediaType="application/xml"/><representation mediaType="application/json"/></request><response><representation mediaType="*/*"/></response></method><resource path="{keyid}/"><param name="keyid" style="template" type="xs:int"/><method id="get" name="GET"><request><param name="expandLevel" style="query" type="xs:int" default="1"/></request><response><representation mediaType="application/json"/></response></method></resource></resource> 

當我特里樹後與NetBeans 「測試RESTful Web服務」 它的工作,這是笏HTTP監視器顯示

狀態:201(創建)

時間標籤:星期六2011年5月21日二十點30分33秒 GMT

發送: { 「作者」: 「邁赫迪」, 「電子郵件」: 「[email protected]」, 「消息」: 「Hello2」}

收稿日期:


請求:POST http://localhost:8080/TRESTful/resources/messages/? 時間戳= 1306009225785

狀態:201(創建)

時間標記:星期六,2011 5月21日20時20分25秒 GMT

發送: { 「作者」: 「邁赫迪」 「電子郵件」: 「[email protected]」, 「消息」: 「Hello2」}

但現在我不知道。首先應該ASIHttpRequest如何我做[要求setRequestMethod:@ 「PUT」]; ?和我應該把所有的JSON像繩子,或把每個值與哎鍵,這樣的:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
[request setPostValue:@"Ben" forKey:@"author"]; 
[request setPostValue:@"Copsey" forKey:@"email"]; 
[request setPostValue:@"Hello2" message:@"author"]; 
[request startSynchronous]; 

或者我應該把JSON字符串中的一個值?

請幫忙。

回答

2

那麼,如果你想送一個JSON你有兩種選擇:

一)發送一個文件 - 使用此,如果你在一個JSON有大量的數據,你可能想存儲它服務器供以後解析。

b)將它作爲字符串中的一個值 - 使用此,如果您的JSON是短,你要分析它立即

編輯

C)將其作爲單獨的值,如果你想跳過服務器上的解析並直接訪問數據。如果數據很少,這應該被使用。

+0

非常感謝 – user567 2011-05-22 03:22:51