iOS通過ASIHTTPRequest提交JSON數據

jopen 10年前發布 | 27K 次閱讀 iOS開發 移動開發 ASIHTTPRequest

先驗知識——什么是ASIHTTPRequest?

 

使用iOS SDK中的HTTP網絡請求API,相當的復雜,調用很繁瑣,ASIHTTPRequest就是一個對CFNetwork API進行了封裝,并且使用起來非常簡單的一套API,用Objective-C編寫,可以很好的應用在Mac OS X系統和iOS平臺的應用程序中。ASIHTTPRequest適用于基本的HTTP請求,和基于REST的服務之間的交互。


如何使用ASIHTTPRequest?


網上有很多專門介紹ASIHTTPRequest使用的文章,很詳細,樓主就不贅述了哈,在此給出一篇經典的介紹詳細的文章鏈接:http://www.cnblogs.com/dotey/archive/2011/05/10/2041966.html

 

上傳JSON格式數據

 

首先給出主功能代碼段,然后對代碼進行詳細解析:

NSDictionary *user = [[NSDictionary alloc] initWithObjectsAndKeys:@"0", @"Version", nil];
                if ([NSJSONSerialization isValidJSONObject:user])
                {
                    NSError *error;
                    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error: &error];
                    NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
                    //NSLog(@"Register JSON:%@",[[NSString alloc] initWithData:tempJsonData encoding:NSUTF8StringEncoding]);

                    NSURL *url = [NSURL URLWithString:@"http://42.96.140.61/lev_version.php"];
                    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
                    [request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
                    [request addRequestHeader:@"Accept" value:@"application/json"];
                    [request setRequestMethod:@"POST"];
                    [request setPostBody:tempJsonData];
                    [request startSynchronous];
                    NSError *error1 = [request error];
                    if (!error1) {
                        NSString *response = [request responseString];
                        NSLog(@"Test:%@",response);
                    }
                }

 

代碼段第一行:

NSDictionary *user = [[NSDictionary alloc] initWithObjectsAndKeys:@"0", @"Version", nil];

構造了一個最簡單的字典類型的數據,因為自iOS 5后提供把NSDictionary轉換成JSON格式的API。

第二行if判斷該字典數據是否可以被JSON化。

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error: &error];
這一句就是把NSDictionary轉換成JSON格式的方法,JSON格式的數據存儲在NSData類型的變量中。

NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];

這一句是把NSData轉換成NSMutableData,原因是下面我們要利用ASIHTTPRequest發送JSON數據時,其消息體一定要以NSMutableData的格式存儲。

下面一句注視掉的語句

//NSLog(@"Register JSON:%@",[[NSString alloc] initWithData:tempJsonData encoding:NSUTF8StringEncoding]);

主要作用是記錄剛才JSON格式化的數據

下面到了ASIHTTPRequest功能部分:

NSURL *url = [NSURL URLWithString:@"http://xxxx"];
                    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
這兩句的主要功能是設置要與客戶端交互的服務器端地址。

接下來兩句:

[request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
                    [request addRequestHeader:@"Accept" value:@"application/json"];
是設置HTTP請求信息的頭部信息,從中可以看到內容類型是JSON。

接下來是設置請求方式(默認為GET)和消息體:

[request setRequestMethod:@"POST"];
                    [request setPostBody:tempJsonData];
一切設置完畢后開啟同步請求:

[request startSynchronous];

最后的一段:

if (!error1) {
                        NSString *response = [request responseString];
                        NSLog(@"Rev:%@",response);
                    }
是打印服務器返回的響應信息。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!