I have a simple app which makes a POST request. And then data is returned. The problem is that the returned data is not JSON..... So how can I view it? Here is my code:

NSString *requestString = [NSString stringWithFormat:@"https://serveraddress.com"];
    NSString *string = [NSString stringWithFormat:@"id=%@&olt_info=%@", @"test", @"renz"];
    NSData *postData = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestString]];
    NSLog(@"\n request str : %@",request);

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSHTTPURLResponse *response = nil;
    NSError *error = nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSLog(@"The responce:\n\n%@", responseData);

    if (error == nil && response.statusCode == 200) {
        NSLog(@"%li", (long)response.statusCode);

        NSError *err;
        id JSon = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err];

        if (err) {
            NSLog(@"%@",err);
        }

        else {
            NSLog(@"Json %@",JSon);

        }
    }

    else {
        //Error handling
        NSLog(@"%@", response);
    }

This is the format of the returned data that I am trying to read:

new_token=509723045780uIRBWRBH24b

So I know the downloaded data gets stored in the NSData called "responseData". But if I print it using NSLog I just get this:

<61636365 73735f74 6f6b656e 3d313538 32353838 33343337 39343431 7c365f4d 6543436e 6b51716f 722d6e70 61746662 484d6458 526b3477>

So how do I read this???

Thank you for your time, Dan.

有帮助吗?

解决方案

To get NSString from NSData use

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

maybe this string will have a valid JSON format.

其他提示

NSString *requestString = [NSString stringWithFormat:@"https://serveraddress.com"];
NSString *string = [NSString stringWithFormat:@"id=%@&olt_info=%@", @"test", @"renz"];
NSData *postData = [string dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestString]];
NSLog(@"\n request str : %@",request);

[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

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



 *id json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];*
  NSLog(@"%@", json);




if (error == nil && response.statusCode == 200) {
    NSLog(@"%li", (long)response.statusCode);

    NSError *err;
    id JSon = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err];

    if (err) {
        NSLog(@"%@",err);
    }

    else {
        NSLog(@"Json %@",JSon);

    }
}

else {
    //Error handling
    NSLog(@"%@", response);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top