我正在尝试调用一个类方法,该方法接受一个字符串并将其发布到一个站点以接收 JSON 响应(在我存储在 DataClass 中的一些其他变量中)。我被困在试图以响应的形式返回数据,此时甚至无法 NSLog 返回的数据。问题是,既然我已经调用了我的类方法,那么类方法如何等待从 HTTP POST 返回响应以返回数据?返回 JSON 后,我可以将其扩展为字典并从那里进行处理。帮助表示赞赏:)
上课方法:
//
// APISample.m
//
// Created by Sam on 1/6/13.
// Copyright (c) 2013 Sam. All rights reserved.
//
#import "APISample.h"
#import "DataClass.h"
@implementation APISample
@synthesize first_name = _first_name;
@synthesize last_name = _last_name;
@synthesize profile_pic_url = _profile_pic_url;
@synthesize responseData;
-(id)init
{
self = [super init];
return self;
NSLog(@"Loaded APISample and fetching");
}
+(id)getDataAboutUser:(NSString *)user_request_id;
{
DataClass *userdata=[DataClass getInstance];
NSLog(@"Loaded APISample and fetching %@", user_request_id);
NSMutableURLRequest *user_fetch_details = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://10.0.23.161/users/user_fetch_details.php"]];
[user_fetch_details setHTTPMethod:@"POST"];
NSMutableString *postString = [NSMutableString stringWithString:@"id=123"];
[postString appendString:@"&userrequest_id="];
[postString appendString:[userdata.str_userid copy]];
[postString appendString:@"&user_id="];
[postString appendString:[userdata.str_userid copy]];
[postString appendString:@"&identifier="];
[postString appendString:[userdata.str_identifier copy]];
[user_fetch_details setValue:[NSString stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"];
[user_fetch_details setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:user_fetch_details delegate:self];
NSMutableData *responseData=[NSMutableData data];
[responseData appendData:[NSURLConnection connection:didReceiveData];
if (connection) {
// Create the NSMutableData that will hold
// the received data
// receivedData is declared as a method instance elsewhere
NSMutableData *responseData=[NSMutableData data];
} else {
// inform the user that the download could not be made
}
NSLog(@"Received Data %@", [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]);
return [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
NSString *receivedDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if ([receivedDataString isEqualToString: @"error"]) {
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"An error has occured. The application will now exit. Unexpected Response!"
delegate:nil
cancelButtonTitle:@"Close"
otherButtonTitles:nil];
[errorAlert show];
exit(0);
}else{
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSString *firstnameResponse = [json objectForKey:@"first_name"];
NSString *lastnameResponse = [json objectForKey:@"last_name"];
NSString *profile_pic_urlResponse = [json objectForKey:@"profile_pic_url"];
NSLog(@"didReceiveData %@ analysed " , firstnameResponse);
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);
}
@end
在“收到数据”之后,我在日志中没有收到任何数据,也没有看到我的错误消息。谢谢