数据库连接问题

时间:2015-10-20 19:07:47

标签: ios

我遇到问题,当我进行数据库查询时,我的成功连接会返回0

NSInteger success;
@try {
    NSString *post = [NSString stringWithFormat:@"&operador=3"];
    NSLog(@"PostData: %@",post);
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
    //Se inicia una peticion de conexion a internet
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    //Se define la url
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://notelapuedomostrar.php"]]];
    //Se define el tipo de envio
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSLog(@"Response code: %ld", (long)[response statusCode]);

    if ([response statusCode] >= 200 && [response statusCode] < 300)
    {
        NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
        NSLog(@"Response ==> %@", responseData);

        NSError *error = nil;
        NSDictionary *jsonData = [NSJSONSerialization
                                  JSONObjectWithData:urlData
                                  options:NSJSONReadingMutableContainers
                                  error:&error];

        success = [jsonData[@"success"] integerValue];
        NSLog(@"Success: %ld",(long)success);

        NSString *datos = [jsonData objectForKey:@"fecha"];
        //int len = [responseData length];

        NSLog(@"Datos del jsonData %@",datos);
        //NSArray *fechaOP = [jsonData objectForKey:@"fecha"];
        //ARRAY
        fechas = [[NSArray alloc] initWithObjects:@"02/10/2015",@"03/10/2015",@"13/10/2015", nil];

        if(success == 1)
        {
            NSLog(@"Viaje SUCCESS");

        } else {

            NSString *error_msg = (NSString *) jsonData[@"error_message"];
            [self alertStatus:error_msg :@"Error en el viaje" :0];
        }

    } else {
        //if (error) NSLog(@"Error: %@", error);
        [self alertStatus:@"Fallo la conexion" :@"" :0];
    }
}
@catch (NSException * e) {
    NSLog(@"Exception: %@", e);
    [self alertStatus:@"Viaje Failed." :@"Error!" :0];
}

我看过其他问题,其中错误为forHTTPHeaderField:@"Current-Type"。在我的情况下,问题来自jsonData返回NULL

NSLog中的输出(@&#34;响应==&gt;%@&#34;,responseData);是:

Response ==> {"success":1,"Viajes":[{"idviajes":"87","hora":"","fecha":"02\/oct\/2015","costo":"$ 14.37 Pesos","ubicacion":"Calle Zempoala 251, 03020 Ciudad de Mexico, DF","destino":"Calle Mitla 200, 03020 Ciudad de Mexico, DF"}]}

请看看和建议吗?

编辑 在此过程中查看我的错误,当前parcear NSJSONSerialization告诉我日志ERROR: The data couldn’t be read because it isn’t in the correct format.

0 个答案:

没有答案
相关问题