Network connection was lost NSURLConnection

2019-09-01 08:35发布

This error is printed every time I attempt a connection. No idea why it cant connect to the server. Built a simple java application and it worked fine. Ive been surfing StackOverflow for days trying to figure this out. Tried multiple API's and somethings just not right. If you need more code, or have any further questions let me know. Please help. :(

2013-08-29 21:56:55.946 panic[15054:70b] Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0xa779a30 {NSErrorFailingURLStringKey=http://54.221.224.251/, NSErrorFailingURLKey=http://54.221.224.251/, NSLocalizedDescription=The network connection was lost., NSUnderlyingError=0xa611130 "The network connection was lost."}

Here is my code that sets up the request:

NSString *urlPath = [NSString stringWithFormat:@"http://54.221.224.251"];
    [urlPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlPath];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *stringdata = [NSString stringWithFormat:@"name=%@&email=%@&phash=%@",name,email,phash];
    NSString *postData = [[NSString alloc] initWithString:stringdata];
    [postData stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.connection = connection;
    [connection start];

Here is my code thats supposed to handle responses from the php server.

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.receivedData appendData:data];
}
/*
 if there is an error occured, this method will be called by connection
 */
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    NSLog(@"%@" , error);
}

/*
 if data is successfully received, this method will be called by connection
 */
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"WORKED!");
}

1条回答
放荡不羁爱自由
2楼-- · 2019-09-01 08:58

Instead of just putting ampersands, I needed to add them in like so for the stringData.

NSString *stringdata = [NSString stringWithFormat:@"name=%@&email=%@&phash=%@",name,email,phash];

This fixed my problem.

查看更多
登录 后发表回答