我想通过使用简单的Objective C Code方法创建HTTP
请求post
和get
响应。下面的代码可以成功发布。但我没有收到回复数据。仅响应打印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);
的响应不是有效的JSON。从responseData创建NSString并打印出来。同时打印err和错误变量。 – 2014-11-25 08:29:34
你可以请张贴一些代码吗?我无法得到你。 – Mano 2014-11-25 08:31:48
NSLog(@“%@”,err);和NSLog(@“%@”,错误); – 2014-11-25 08:32:38