2016-02-22 106 views
1

我想構建一個郵件正文中的某些參數的POST請求,但我正在努力處理/看到響應。來自POST請求的響應?

這是我想在的OBJ C鍵重新請求:

enter image description here

我有些熟悉的代碼來創建此請求,但正與解析響應轉換成有意義的數據掙扎。這是我的方法目前看起來像這樣(它似乎是成功的):

NSLog(@"WEB SERVICE CALLED!"); 

    NSURL *aUrl = [NSURL URLWithString:@"xxx"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy 
                 timeoutInterval:60.0]; 

    [request setHTTPMethod:@"POST"]; 
    NSString *postString = @"grant_type=password&username=xxx&password=xxx"; 
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request 
                   delegate:self]; 

    if(connection) { 
     NSLog(@"Connection Successful"); 
    } else { 
     NSLog(@"Connection could not be made"); 
    } 
} 

我怎樣才能看到我的迴應是什麼?預先感謝任何幫助!

UPDATE

我加入此位,可以看到,我的回答給了我一個代碼200(成功),但數據對象看起來需要,因爲它看起來像一個十六進制字符串被序列化。這是我在做什麼處理響應/數據/錯誤:

[NSURLConnection sendAsynchronousRequest:request 
            queue:[NSOperationQueue mainQueue] 
         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 

          // your data or an error will be ready here 
          NSLog(@"RESPONSE: %@",response); 

          NSLog(@"DATA: %@",data); 

          NSLog(@"ERROR: %@",error); 
         }]; 

這是在日誌中的數據對象的樣子: enter image description here

+0

http://stackoverflow.com/問題/ 25701436 /如何使用post-nsurlrequest-with-2參數 –

+0

查看上面的更新 – arcade16

+0

將數據轉換成Json –

回答

5
NSError *error; 
    id obj= [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 
    if (!obj) { 
     NSLog(@"JSON parse error: %@", error); 
    } else { 
     NSLog(@"obj= %@", obj); 
    }