开发者

ASIHTTPRequest only works when i hardcode the strings

I have a problem that really makes me scratch my head. There seems to be no logical reason why this is happening. If i hardcode the string into the NSURL it works. But if I pass a NSString instead it doesnt work. I have confirmed that the NSString is identical to hardcoded string by NSLog both of them.

Code that works. Server returns a 200 responce code.

        //NSString * urlStr = [NSString stringWithFormat:@"http://www.server.com/%@",monograph.uri];
    NSURL *url = [NSURL URLWithString:@"http://www.server.com/api/monograph/com/3894.json"];

    ASIHTTPRequest *request; 
    request = [ASIHTTPRequest requestWithURL:url];
    [request setUsername:appDelegate.key];
    [request setPassword:appDelegate.secret];
    [request setDelegate:self];
    [request startAsynchronous];

Code that fails.开发者_开发知识库 Server returns 204 response code for this:

NSString * urlStr = [NSString stringWithFormat:@"http://www.server.com/%@",monograph.uri];
    NSURL *url = [NSURL URLWithString:urlStr];

    ASIHTTPRequest *request; 
    request = [ASIHTTPRequest requestWithURL:url];
    [request setUsername:appDelegate.key];
    [request setPassword:appDelegate.secret];
    [request setDelegate:self];
    [request startAsynchronous];

When i NSLog urlStr it contains:

http://www.server.com/api/monograph/com/3894.json

What am i missing? There must be something to explain it -.-

Regards, Code

EDIT

checked the length of both strings and they are both same length so seems not white spaces or anything hidden in there.


Have you checked whether the NSURL object you get from the string variable is not nil? That could help you home in on the location of the problem.

Also, you might try escaping the string variable, just incase something funny is getting in with the stringWithFormat call.


Cut all the new lines/spaces from the string, this is one possible reason. And sometimes the log doesn't show you the new lines.

try

NSString * s = [monograph.uri stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSString * urlStr = [NSString stringWithFormat:@"http://www.server.com/%@",s];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜