Objective-C and ASIHTTPRequest - response string problems

Go To StackoverFlow.com


I'm using the ASIHTTPRequest package to send some data from the iPhone to my server and then when saved on server I recieve a response (a string) containing a url that I want to load in a webview. In theory this should be simple, but in reality I can't get this work at all. Seems to be some problems with encoding of the string I guess. If I NSLog out the response it seems to be totally fine, but it just refuses to load in the webview.

This is my request (been playing around with compression and encoding):

NSURL *url = [NSURL URLWithString:@"xxx"];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setFile:imageData forKey:@"photo"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
[request setAllowCompressedResponse:NO];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];//NSISOLatin1StringEncoding   
[request start];

And this is my responder:

- (void)requestDone:(ASIHTTPRequest *)request{
    NSString *response = [request.responseString];
    NSLog(@"web content: %@", response);
    [webView loadSite:response];

The NSLog seems fine, but site just won't load. Tried sending an NSString variable like NSString *temp = @"http://www.google.se"; to the loadSite function and that works fine, so the problem is not with the loading itself.

Would greatly appriciate any pointers or help in the obj-c jungle :)


2009-06-16 07:57
by NoName


Try to do the following:

NSString *response = [[request responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

It will trim all whitespaces and newlines.

2009-06-16 11:16
by Valerii Hiora
awesome! thanks a lot! much nicer than the substring solution i ended up with - NoName 2009-06-16 14:51


so turns out that a response is automatically ended with a \n (new linebreak). this didn't show up when logging the variable, neither when writing it to a textfield but when converting it to a urlobject and logging that i found it. debugging once again pays off ;)

2009-06-16 09:59
by NoName


woups, my responder is of course:

- (void)requestDone:(ASIHTTPRequest *)request{
    NSString *response = [request responseString];
    NSLog(@"web content: %@", response);
    [webView loadSite:response];
2009-06-16 08:07
by NoName