NSURLConnection无效

时间:2013-06-07 08:27:18

标签: objective-c iphone nsurlconnection nsurlrequest

我需要从xcode打开我的php网址,我在网址中传递一个变量。我正在执行以下代码,但它无法正常工作,我已经检查过在网络浏览器中输入此网址是否有效。

NSString *urlformatted = [NSString stringWithFormat:@"http://arcamm.uc3m.es/notifications/saveID.php?id=%@",deviceToken];

    NSLog(@"web service URL: %@", urlformatted);

    [urlformatted stringByAppendingString:[urlformatted stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: urlformatted] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest: request delegate:self startImmediately:YES];
    NSLog(@"connection: %@", [connection debugDescription]);
    if (connection) {
        NSLog(@"Connection succeeded");
    } else {
        NSLog(@"Connection failed");
    }

它告诉我连接成功但变量没有出现在我的数据库中。而且我也得到了deviceToken的正确值。

编辑:发现我的错误,我收到了deviceToken格式,所以我做了以下scape<>和“”

NSString *deviceTokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];

1 个答案:

答案 0 :(得分:0)

编辑:发现我的错误,我收到了deviceToken格式,所以我做了以下scape&lt;&gt;和“”

NSString *deviceTokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];