2010-12-20 51 views
4

我正在開發一個iPad項目,並且該項目需要與json-rpc webservices進行交流。 web服務是基於Drupal的與模塊:CCK和視圖如何從json-rpc webservice獲取數據:iPad/iPhone/Objective C

1)I需要推動JSON對象到web服務 2)I需要從web服務回調數據

我已經實施了SBJSON API以及iPad項目的https://github.com/samuraisam/DeferredKit/ api。

的SBJSON API工作正常,我明白這一個 的Samuriaisam DefferedKit是新的,我

我的問題是如何將數據從這個JSON-RPC web服務的,有別人一些示例代碼?或者在某些地方我可以找到Objective C - json-rpc webservice文檔。

親切的問候,

巴特Schoon

--------- --------更新

我現在使用此代碼:

NSString *jsonString = @"{\"method\":\"views.get\",\"params\":{\"view_name\":\"client_list\",\"sessid\":\"xxxxxx\"},\"id\":1}"; 
    NSString *requestString = [NSString stringWithFormat:@"%@",jsonString,nil]; 

    NSLog(@"input: %@",jsonString); 

    NSData *requestData = [NSData dataWithBytes: [jsonString UTF8String] length: [jsonString length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://subdomain.domain.com/services/json-rpc"]]; 

    NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]]; 
    [request setHTTPMethod: @"POST"]; 
    [request setValue:@"Content-type: application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:requestData]; 

    //Data returned by WebService 
    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding]; 
    NSLog(@"output: %@",returnString); 

這將導致此消息來自服務器:

{"error":{"name":"JSONRPCError","code":-32600,"message":"The received JSON not a valid JSON-RPC Request"},"version":"1.1"} 

--------- /更新--------

有什麼不對?有人有此經驗嗎?

親切的問候,

巴特Schoon

回答

0
-(IBAction)testCall{ 
    NSString *requestString = [NSString stringWithFormat:@"method=views.get&view_name=client_list",nil]; 
    NSLog(requestString); 


    NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]]; 


    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://.xxxxxxxxx.nl/services/json"]]; 

    NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]]; 
    [request setHTTPMethod: @"POST"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody: requestData]; 

    //Data returned by WebService 
    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding]; 

    NSLog(returnString); 
    NSDictionary *dict = [returnString JSONValue]; 

    } 

只要不使用一個JSON-RPC - 保持它的簡單和JSON正常JSON方法)

1

讀取JSON文件獲得這些數據。

NSDictionary * dictionary = [jsonString JSONValue]; 您將獲得密鑰&值對。將該數據存儲在各自的變量中。

+0

謝謝你的帖子, 不正是我期待的,我貼了一些上面的新代碼。你有一些建議嗎? 親切的問候, 巴特肖恩 – 2010-12-21 09:39:06

+0

嗨巴特,你檢查JSon文件,你收到它是否有效。否則,您可以嘗試與其他JSon文件臨時用途。不過,如果你會得到相同的錯誤,那麼看看這個鏈接:http://groups.google.com/group/json-rpc/web/json-rpc-1-2-proposal – 2010-12-21 10:39:34

+0

好吧,它的工作原理。我已經選擇在Drupal中使用普通的json方法而不是rpc版本。所以現在我發佈一些POST變量到服務器並接收一個json字符串返回。 Thx幫助我! – 2010-12-24 14:18:48

相關問題