2014-11-25 25 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