我想通過使用簡單的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