我正在爲我的iOS應用程序使用Web服務。 我知道如何通過URL(不是http Body)發送http post請求並使用NSURLConnection Delegate獲取響應。 但現在有更好的方法,我正在跟蹤Web服務並在請求體中傳遞參數。我在谷歌搜索圖書館,發現this。HttpRequest iOS中的Body參數
但是,代碼對我來說有點困難,我想應該有相同的功能,這可以使這一點更容易。 有沒有?到目前爲止的代碼如下。
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL
URLWithString:**WebService URL**]];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/json" forHTTPHeaderField:@"Content-type"];
NSString *jsonString = [NSString stringWithFormat:@"{\n\"username\":\"%@\",\n\"%@\":\"%@\",\n\"%@\":\"%@\",\n\"%@\":%@,\n\"%@\":%@,\n\"version\":%@,\n\"name\":\"%@\"\n}",userName, @"password",pass,@"accessToken",token,@"isOnline",@"True",@"accountType",type,@"False",name];
[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
此代碼中的jsonString是http正文。我想在請求體中傳遞參數。 現在,執行此代碼後,我在NSData變量中得到空值。而我的web服務函數返回一個值,如果執行成功,它也會返回。 我的代碼有什麼問題。 謝謝。
@xmd是iOS中基礎庫的JSON.h部分? – Supertecnoboff 2014-04-15 20:49:17
不,您可以從這裏驗證[link](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/ObjC_classic/_index.html#//apple_ref/doc/uid/20001091) 。 – xrnd 2014-04-22 11:58:29