-2

I am trying to replace my NSMutableURLRequest with an ASIHTTPRequest, I have tried the following below, but when I try this my app crashes. I get no errors just a warning:

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

How do I fix this, what I tried is below and I have commented out what I was replacing:

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];

extra code:

-(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]];
    }

}
user979331
  • 11,039
  • 73
  • 223
  • 418
  • 5
    Really, are you going to use a [dead library](http://allseeing-i.com/[request_release];)? – Marcelo Apr 04 '15 at 02:22
  • 2
    I'd discourage you from using `ASIHTTPRequest`. If you want to use a third party library, nowadays many people gravitate to [AFNetworking](https://github.com/AFNetworking/AFNetworking). – Rob Apr 04 '15 at 02:33
  • This is a horrible decision.... seriously. I also discourage AFNetworking btw.... heavily discourage it, but this particular library is a no brainer don't use. – TheCodingArt Apr 06 '15 at 02:26

2 Answers2

1

You really don't want to use this code.

//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] already performed the request and received the response, all you needed to do was get the response out of the ASI object. NOTE: none of the -connection:didReceiveResponse:, -connection:didReceiveData:, -connectionDidFinishLoading: callbacks will be made, any work done in -connectionDidFinishLoading: will need to be done in the TODO: section I put above.


NOTE: This is bad code. -startSynchronous blocks the thread until the request has completed. This will lock up your UI until the response has completed.

  • Don't use -startSynchronous look into -startAsynchronous.
  • Don't use ASIHTTPRequest use AFNetworking.
  • Think about a better way to refactor this solution!
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • I do the connections methods available....how do I call them ? I am trying your example and it appears I am on the right track, but I just dont know what to do next. I add my connection methods to the question – user979331 Apr 06 '15 at 17:19
0

I did copy some code from PerformanceTests.m of iPhone Xcode project (https://github.com/pokeb/asi-http-request):

- (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]]);
    }
}

I hope it will help you.

stosha
  • 2,108
  • 2
  • 27
  • 29