2015-04-04 46 views
-2

我试图用ASIHTTPRequest替换我的NSMutableURLRequest,我尝试了下面的内容,但是当我尝试这个时,我的应用崩溃了。我没有得到任何错误,只是一个警告:objective-c用ASIHTTPRequest替换NSMutableURLRequest

不兼容的指针类型发送“ASIHTTPRequest *”到类型的参数“的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]]; 
    } 

} 
+5

真的,你打算使用[dead library](http://allseeing-i.com/ [request_release];)吗? – 2015-04-04 02:22:14

+2

我不鼓励你使用'ASIHTTPRequest'。如果你想使用第三方库,现在很多人都倾向于[AFNetworking](https://github.com/AFNetworking/AFNetworking)。 – Rob 2015-04-04 02:33:26

+0

这是一个可怕的决定....认真。我也劝阻AFNetworking顺便说一句....沉重打击它,但这个特定的图书馆是一个没有脑子不使用。 – TheCodingArt 2015-04-06 02:26:20

回答

1

你真的不想使用这段代码。

//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:将不会回调,在-connectionDidFinishLoading:中完成的任何工作都需要在我上面提到的TODO:部分完成。


注:这是错误代码。 -startSynchronous在请求完成之前阻塞线程。这将锁定您的用户界面直到响应完成。

  • 请勿使用-startSynchronous调查-startAsynchronous
  • 请勿使用ASIHTTPRequest使用AFNetworking
  • 想想更好的方法来重构这个解决方案!
+0

我做的连接方法可用....我怎么称呼他们?我正在尝试你的榜样,看起来我在正确的轨道上,但我只是不知道下一步该做什么。我将我的连接方法添加到 – user979331 2015-04-06 17:19:39

+0

这个问题上,我没有想到它。 – user979331 2015-04-06 17:26:12

0

我没有从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]]); 
    } 
} 

我希望这会帮助你。

+0

它有点儿,我添加addRequestHeader,但我仍然得到那个警告和我的应用程序崩溃 – user979331 2015-04-04 07:09:07