如何使用REST从iOS发送/获取API?

时间:2014-06-03 10:26:22

标签: ios objective-c rest

我是iOS开发的新手。我正在尝试开发基本的登录系统。我需要从托管在服务器中的PostgreSQL中获取用户。我是通过REST API完成的,但无法获得响应。

我在下面的代码中附上了我所写的内容。

-(IBAction)savedata:(id)sender
{
    NSString *queryString=[NSString stringWithFormat:@"`http://localhost/tolcys/login/`id=%@,username=%@,password=%@",[self.idtext text],[self.usertextf text],[self.passwordtextf text]];
    NSMutableURLRequest *theRequest=[NSMutableURLRequest
                                     requestWithURL:[NSURL URLWithString:
                                                     queryString]
                                     cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:60.0];
    NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                    @"id", @"Key1",
                                    @"username", @"Key2",
                                    @"password", @"Key3",
                                    nil];
    NSError *error;
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary
                                                       options:NSJSONWritingPrettyPrinted error:&error];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [theRequest setHTTPBody:jsonData];
     NSData* responseData = nil;
    NSURLResponse* response;

    responseData=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
    NSLog(@"%@",responseData);



NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

   NSLog(@"the final output is:%@",responseString);


    NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:jsonData
                                                        encoding:NSUTF8StringEncoding]);
    NSLog(@"JSON sumary:%@",[[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]);
    data1=[[NSMutableData alloc]init];
    //[NSURLConnection connectionWithRequest:theRequest delegate:self];
    // NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    [NSURLConnection connectionWithRequest:theRequest delegate:self];
//    if (con) {
//        _receivedData=[NSMutableData data];
//    } else {        //something bad happened
//   }
////    [self connection:con didReceiveResponse:self.receivedData];
////    [self connection:con didReceiveData:self.receivedData];
////
//    [self fetchGreeting1];



    //[self connection:co didReceiveResponse:<#(NSURLResponse *)#>]

    //NSData* responseData = nil;
    NSURL *url=[NSURL URLWithString:[queryString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];

    responseData = [NSURLConnection sendSynchronousRequest:theRequest     returningResponse:&response error:&error];
    NSString *responseString2 = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"the final output is:%@",responseString2);

}

0 个答案:

没有答案