objective-c 将 NSMutableURLRequest 替换为 ASIHTTPRequest

objective-c Replacing NSMutableURLRequest with ASIHTTPRequest

我正在尝试用 ASIHTTPRequest 替换我的 NSMutableURLRequest,我已经尝试了下面的方法,但是当我尝试这个时我的应用程序崩溃了。我没有收到任何错误,只是一个警告:

Incompatible pointer types sending 'ASIHTTPRequest *' to parameter of type 'NSURLRequest *'

我该如何解决这个问题,下面是我尝试过的,我已经注释掉了我要替换的内容:

receivedData = [[NSMutableData alloc]init];
//NSURL *JSONURL = [NSURL URLWithString:url];

NSURL *url = [NSURL URLWithString:url];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setUseSessionPersistence:YES];
[request setUseKeychainPersistence:NO];
[request setUsername:@"username"];
[request setPassword:@"password"];
[request setDomain:@"domain"];
[request startSynchronous];

//NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
jobsConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[jobsConnection start];

额外代码:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [receivedData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

    //NSLog(@"%@" , error);
    communityDictionary = nil;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *myError;
    if([connection isEqual:jobsConnection])
    {
        communityDictionary = [[NSDictionary alloc]initWithDictionary:
                               [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&myError]];
    }

}

我确实从 iPhone Xcode 项目 (https://github.com/pokeb/asi-http-request) 的 PerformanceTests.m 复制了一些代码:

- (void)startASIHTTPRequests
{
    bytesDownloaded = 0;
    [self setRequestsComplete:0];
    [self setTestStartDate:[NSDate date]];
    int i;
    for (i=0; i<10; i++) {
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:testURL];
        //Send the same headers as NSURLRequest
        [request addRequestHeader:@"Pragma" value:@"no-cache"];
        [request addRequestHeader:@"Accept" value:@"*/*"];
        [request addRequestHeader:@"Accept-Language" value:@"en/us"];
        [request setDelegate:self];
        [request startAsynchronous];
    }
}    

- (void)requestFailed:(ASIHTTPRequest *)request
{
    GHFail(@"Cannot proceed with ASIHTTPRequest test - a request failed");
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    bytesDownloaded += [[request responseData] length];
    requestsComplete++;
    if (requestsComplete == 10) {
        NSLog(@"ASIHTTPRequest: Completed 10 (downloaded %lu bytes) requests in %f seconds",bytesDownloaded,[[NSDate date] timeIntervalSinceDate:[self testStartDate]]);
    }
}

希望对你有所帮助

您真的不想使用此代码。

//receivedData = [[NSMutableData alloc]init]; // <-- Not needed
//NSURL *JSONURL = [NSURL URLWithString:url];

NSURL *url = [NSURL URLWithString:url];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setUseSessionPersistence:YES];
[request setUseKeychainPersistence:NO];
[request setUsername:@"username"];
[request setPassword:@"password"];
[request setDomain:@"domain"];
[request startSynchronous];

// You are already done getting the request, you just need to handle the response.
NSError *error = [request error];
if (!error) {
    // Put the response data into your received data object.
    receivedData = [[request responseData] mutableCopy];
    // TODO: copy in the code from -connectionDidFinishLoading:
} else {
    NSLog(@"%@", error);
}

// There is no need to do anything with NSURLConnection.
//NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
//jobsConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//[jobsConnection start];

[request startSynchronous] 已经执行了请求并收到了响应,您需要做的就是从 ASI 对象中获取响应。注意:将进行 -connection:didReceiveResponse:-connection:didReceiveData:-connectionDidFinishLoading: 回调中的 none,在 -connectionDidFinishLoading: 中完成的任何工作都需要在 [=16] 中完成=] 我放在上面的部分。


注意: 这是错误的代码。 -startSynchronous 阻塞线程直到请求完成。这将锁定您的 UI,直到响应完成。

  • 不要使用 -startSynchronous 查看 -startAsynchronous
  • 不要使用 ASIHTTPRequest 使用 AFNetworking.
  • 想想重构这个解决方案的更好方法!