2017-03-03 3 views
0

NSURLConnection 메서드를 사용하여 웹 서비스를 호출하고 싶습니다. 제 3 자 라이브러리 AFNetworking으로 대체하고 싶습니다. 어떻게해야합니까? 먼저 웹에서 데이터를 호출합니다. 여기서 시간은 내가 현재하고있는 일이다. 여기Objective C에서 AFNetworking을 사용하여 웹 서비스 호출하기

- (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은 사용자 이름과 암호처럼 내 암호화 된 데이터입니다. 미리 감사드립니다. !

답변

0

웹 서비스에 POST 요청을 보내야합니다. 이전 코드 스 니펫을 사용할 수 있습니다.

NSString *post = [NSString stringWithFormat:@"%@&value=%@&value=%@&value=%@&value=%@&value=%@",self.Comment_Memberid,self.SharedID,self.Post_Ownerid,self.commentText.text,email_stored,password_stored]; 

    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://.../api/json/postcomment/"]]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"text/plain; charset=utf-8" forHTTPHeaderField:@"Content-type"]; 
    //[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"]; 
    [request setHTTPBody:postData]; 
    //get response 
    NSHTTPURLResponse* urlResponse = nil; 
    NSError *error = [[NSError alloc] init]; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    NSLog(@"Response Code: %ld", (long)[urlResponse statusCode]); 

    if ([urlResponse statusCode] == 200) 
    { 

    } 

그리고 당신은 당신이 코드 아래 사용할 수 있습니다 AFNetworking와 함께 사용하여 POST 요청을 보내려면 :

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 
NSDictionary *params = @{@"34": x, 
         @"130": y}; 
[manager POST:@"https://../api/json/cordinates" parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) { 
    NSLog(@"JSON: %@", responseObject); 
} failure:^(NSURLSessionTask *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
}]; 
+0

내 데이터는 –

+0

는 중요하지 않습니다 간단한 TextPlain입니다. 위 코드를 사용하여 데이터를 웹 서비스에 전달해야합니다. 데이터는 이미지 (NSData) 또는 단순한 순수 문자열 일 수 있습니다. 이 코드 섹션을 사용할 수 있습니다. –