0

我想在我的应用程序中进行延迟加载,所以我正在使用 NSURLConnection 下载图像,如您所见

premiumRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"www.aaaaaaaaa.com/aaaaaaaaa/api/uploads/company_logo/cc2ab63fd3eb564be64b4f21bd083bc7.png"]
                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                timeoutInterval:60.0];

premiumConnection=[[NSURLConnection alloc] initWithRequest:premiumRequest delegate:self];

但不幸的是我收到以下错误

error downloading: unsupported URL

如果将 url 粘贴到浏览器上它可以正常工作,但它在 NSURLConnection 中不起作用,请帮忙看看上面的 URL 有什么问题

4

3 回答 3

3

尝试在您的 url 中包含适当的 url 方案,例如

...[NSURL URLWithString:@"http://www...
于 2012-05-02T07:15:33.900 回答
1
NSURL *url = [NSURL URLWithString:@"http://www.invoicera.com/app/api/check_json_api.php?token=CFBF57B78FB183157BF93F0EB00C9C33"];

NSString *jsonRequest = [NSString stringWithFormat:@"&json_data=%@",[[NSString stringWithFormat:@"yourString"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSLog(@"%@",jsonRequest);

NSData *json_data = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

NSLog(@"The converted String is %@",json_data);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];


[request setHTTPMethod:@"POST"];
[request setHTTPBody: json_data];
NSLog(@"%@",json_data);
// [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [json_data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
                      dataUsingEncoding:NSUTF8StringEncoding 
                      allowLossyConversion:YES]];

// [NSURLConnection connectionWithRequest:[request autorelease] delegate:self];

NSURLConnection *nsUrlConnection=[[NSURLConnection alloc]
                                  initWithRequest:request 
                                  delegate:self];

// Successful connection.
if (nsUrlConnection) {

    [self indicatorView];
   // [self initSpinner];
   // [self spinBegin];

    NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData=data;
    [data release];
} 
// Unsuccessful connection.
else {

}  
// Clean up
[url release];
[request release];

之后正确设置属性,您将收到任何错误。

按答案的向上箭头给答案投票。

于 2012-05-02T07:18:37.860 回答
0
    NSURL *webURL = [NSURL URLWithString:@"YOur URL here"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:webURL];
    [请求 setTimeoutInterval:60.0];
    [请求 setHTTPMethod:aStrGetOrPost];
    NSError *错误;
    NSURLResponse *响应;

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returnedResponse:&response error:&error];

    如果(返回数据)
        {
            //对数据做你想做的事
        }
        别的
        {
            //显示失败提示
        }

于 2012-05-02T07:18:01.653 回答