我想調用一個類方法,它需要一個字符串並將其發佈到一個網站以接收JSON響應(在我存儲在DataClass中的一些其他變量中)。我被困在試圖以響應的形式返回數據,並且在這一點上甚至無法NSLog返回的數據。現在的問題是,現在我調用了我的類方法,類方法如何等待返回來自HTTP POST的響應以返回數據?一旦我返回我的JSON,我可以將其擴展爲字典並從那裏進行處理。幫助表示讚賞:)iOS應用程序在調用方法時等待HTTP響應?
類方法:
//
// 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
我收到日誌中沒有數據「接收數據」後,並沒有看到我的錯誤消息。謝謝
我會盡力你們倆給我的建議和報告今晚回來!感謝您的詳細信息和幫助。一個快速跟進,因爲我將在運行時調用多個連接來獲取用戶界面的不同部分,並且不希望我的用戶在每個人獨立加載時等待,對於這些類進行線程化會更好嗎?或者是塊變量提到最好的方法?再次感謝! – user1955162
你當然想要在後臺線程上做任何耗時的事情。主線程發生的任何事情都將阻止所有用戶交互以及UI更新。但是親自產生NSThreads是很少必要的。因爲NSURLConnection會自動創建和管理自己的線程。所以我只想讓它做它自己的事情 – bturner