3

I'm having this problem for a long while now and i can't get it solved. What happens is, when i do a post request from my iPhone code i get the data back from the server, and it's supposed to be in "UTF format", but it sometimes is and sometimes just, isn't? I know this cause it seems that, when converting data bytes to string using encoding, i sometimes get a nil result, meaning string was not in the give encoding. I put this code in a loop and after a few tries, i get my string in "utf8" proper encoding, but even then there is this "tail" of unknown bytes after the original string, like lets say server data should end with --af~~ but instead it ends with --af~~∆^ø or something like that.

php side :

 <?php
include 'config.php';
mysql_query("SET NAMES utf8"); 


$sifra = $_POST['blabla1'];
$stol =$_POST['blabla2'];
$uname = $_POST['blabla3'];
$ug=array();
$sql=mysql_query("select * from blabla where blabla = '".$sifra."' ");
while($row=mysql_fetch_assoc($sql))
{
    $sql2 = mysql_query("select id from blabla where blablabla=".$row['id']." and blabla = '".$stol."';");
    while($row2 = mysql_fetch_assoc($sql2))
    {
        $ug = $row;
        echo "true ug:~".$ug['name']."#~".$ug['website_path']."#id~".$ug['id'];
    }

}

mysql_query("update blabla");

mysql_close();
?>

The blablas are there as placeholders for real values that i want kept private

obj c side:

+(NSString*)getPhpDataFrom:(NSString*)addr with:(NSString*)vals
{
    for (NSInteger i = 0; i < 5; i = i+1) {
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"blabla"]]];

       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
        [request setValue:@"utf-8" forHTTPHeaderField:@"charset"];
        NSString *body = vals;

        NSData *data2=[body dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:data2];
        [request setHTTPMethod:@"POST"];

        NSError *error = nil; NSURLResponse *response = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        if (error) {
            NSLog(@"Error:%@", error.localizedDescription);
        }
        else {
            //success
            NSString* result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            if([result rangeOfString:@"\n"].location != NSNotFound)
            result = [result substringToIndex:[result rangeOfString:@"\n"].location];

            if(result != NULL || [result isEqualToString:@""]){

                return result;
            }

            else continue;

        }
    }

    return nil;
}

I'm struggling with this for a long time now, hopefully some1 can direct me :(

Edit:

I should also mention, there is a included php file called "config.php" as u can see, i got this inside as well, doesn't help

mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $connect);

php itself is also encoded in utf8

Edit2: So i did this:

NSLog(@"%@",[data bytes]);

And what i got printed out is:

Printing description of data->isa:
__NSCFData
Printing description of data:
<74727565 2075673a 7e417175 616d6172 696e237e 61717561 6d617269 6e236964 7e310a>

I don't get what those values should be?

here is the memory view of data object memory view of data object

captured net traffic gives me this, wth :S

enter image description here

Alen Zarkovic
  • 224
  • 2
  • 11
  • what i don't get is, i got the same thing done in android, and it works perfectly. :S – Alen Zarkovic May 15 '14 at 13:48
  • is it possible that NSURLConnection sendSynchronousRequest is causing this? cuz when i log the data, i get a nil printed out, but when i hover my mouse over it it says 35bytes – Alen Zarkovic May 15 '14 at 14:00
  • edited my original post, pls take a look – Alen Zarkovic May 15 '14 at 14:08
  • same thing, this is what data outputs 2014-05-15 16:14:21.021 blabla[421:60b] <74727565 2075673a 7e417175 616d6172 696e237e 61717561 6d617269 6e236964 7e310a> – Alen Zarkovic May 15 '14 at 14:14
  • NSLog(@"data: %@", data); = 2014-05-15 16:14:21.021 blabla[421:60b] data: <74727565 2075673a 7e417175 616d6172 696e237e 61717561 6d617269 6e236964 7e310a> – Alen Zarkovic May 15 '14 at 14:20
  • also, i added an image of memory block of data object in the original post, maybe it helps spot the issue – Alen Zarkovic May 15 '14 at 14:27
  • Examine exactly what is coming back from the server with a network analyzer such as Charles Proxy. Look to see the "Content-Type". The return data may not be UTF-8, could be UTF-16 or another encoding. Yep, you will have to learn about what is actually send/received, that knowledge will serve you well. Also `NSLog(@"%@",[data bytes]);` is incorrect, there is a type mis-match. – zaph May 15 '14 at 15:09
  • ok so i examined it, and what i got is in the original post, now I'm 100% confused. – Alen Zarkovic May 15 '14 at 15:57

1 Answers1

0

You could try creating a json object & base64 encoding it on both ends.

NSData Class Reference - base64EncodedStringWithOptions

Stack Overflow - NSJSONSerialization

Community
  • 1
  • 1
henryallsuch
  • 169
  • 1
  • 4