Network connection was lost NSURLConnection

这一生的挚爱 提交于 2019-12-11 20:54:16

问题


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:


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.



来源:https://stackoverflow.com/questions/18523807/network-connection-was-lost-nsurlconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!