2015-04-04 47 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