0
我正在使用NSURLConnection方法調用Web服務,我希望將其替換爲第三方庫AFNetworking我應該如何實現這一點我正在使用數據調用Web服務這裏的時間就是我目前所做的。在AFNetworking的幫助下調用Web服務目標C
- (void)viewDidLoad {
[super viewDidLoad];
NSURL * linkUrl = [NSURL URLWithString:@URLBase];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:linkUrl];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[str length]];
[theRequest addValue: @"text/plain; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [str dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection)
{
webData = [[NSMutableData alloc] init];
}
else
{
NSLog(@"theConnection is NULL");
}
}
#pragma mark -- Connection Delegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//NSLog(@"%@",response);
//[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"\nERROR with theConenction");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseDataString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSLog(@"Converted NSData %@ ",responseDataString);
}
這裏
[theRequest setHTTPBody: [str dataUsingEncoding:NSUTF8StringEncoding]];
str是我喜歡的用戶名和密碼進行加密的數據。 在此先感謝。
我的數據是簡單TextPlain –
無所謂。您應該使用上面的代碼將數據傳遞給Web服務。你的數據可以是圖像(NSData)或者只是一個純字符串。你可以使用這段代碼。 –