2014-11-25 52 views
0

我想通過使用簡單的Objective C Code方法創建HTTP請求postget響應。下面的代碼可以成功發佈。但我沒有收到回覆數據。僅響應打印null值。請幫助我,我需要獲取響應數據。HTTP請求顯示NULL值的GET響應

這裏我下面的代碼

NSString *post = [NSString stringWithFormat:@"name=%@",name]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; 
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:URLPATH]]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:postData]; 
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 

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



// Create GET method 
NSError *err; 
NSURLResponse *responser; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responser error:&err]; 

// JSON Formatter 
NSError *error; 
NSDictionary *jsonsDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 
NSDictionary *respon = [jsonsDictionary objectForKey:@"response"]; 
NSLog(@"UPDATE RESPONSE : %@", respon); 
+0

的響應不是有效的JSON。從responseData創建NSString並打印出來。同時打印err和錯誤變量。 – 2014-11-25 08:29:34

+0

你可以請張貼一些代碼嗎?我無法得到你。 – Mano 2014-11-25 08:31:48

+0

NSLog(@「%@」,err);和NSLog(@「%@」,錯誤); – 2014-11-25 08:32:38

回答

0

它非常簡單:

//-- Convert string into URL 
    NSString *jsonUrlString = [NSString stringWithFormat:@"demo.com/your_server_db_name/service/link"]; 
    NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    //-- Send request to server 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url]; 
    //-- Send username & password for url authorization 
    [request setValue: jsonUrlString forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPMethod:@"POST"]; //-- Request method GET/POST 

    //-- Receive response from server 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

    //-- JSON Parsing with response data 
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil]; 
    NSLog(@"Result = %@",result); 

樣品:https://github.com/svmrajesh/Json-Sample

+1

爲什麼在已經有數百個如何將JSON轉換爲已在本網站上的Objective-C集合的示例中發佈這個內容? – trojanfoe 2014-11-25 08:48:45

+0

什麼是authValue? – Mano 2014-11-25 09:07:53

+0

不要使用這段代碼,我可以看到它的一堆bug。你的代碼沒有任何錯誤,你只需要找出服務器響應的數據。它返回一個無效的響應。 – 2014-11-25 09:15:36

0

您的數據後爲NSString *post = [NSString stringWithFormat:@"name=%@",name];。因此,在這種情況下,你需要設置請求頭"Content-Type""application/x-www-form-urlencoded"或使用下面的代碼字符串轉換爲數據,並設置Content-Type頭:

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 
... 
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
+0

可能不需要請求標頭。這取決於服務器的期望,有些會忽略Content-Type頭。他正在從服務器獲得響應,並需要讓代碼處理該問題。 – 2014-11-25 09:14:38

+0

我不認爲在這種情況下不需要。因爲它內容正文數據格式信息 – larva 2014-11-25 09:20:50

+0

請發佈任何有用的信息... – Mano 2014-11-25 09:32:45