网络连接丢失 NSURLConnection [英] Network connection was lost NSURLConnection

查看:88
本文介绍了网络连接丢失 NSURLConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次尝试连接时都会打印此错误.不知道为什么它无法连接到服务器.构建了一个简单的 java 应用程序,它运行良好.我已经在 StackOverflow 上冲浪了好几天试图解决这个问题.尝试了多个 API 和一些不正确的东西.如果您需要更多代码,或有任何其他问题,请告诉我.请帮忙.:(

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] 错误域=NSURLErrorDomain代码=-1005 网络连接丢失."用户信息=0xa779a30{NSErrorFailingURLStringKey=http://54.221.224.251/,NSErrorFailingURLKey=http://54.221.224.251/,NSLocalizedDescription=网络连接丢失.,NSUnderlyingError=0xa611130 "网络连接丢失."}

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

这是我的代码,用于处理来自 php 服务器的响应.

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!");
}

推荐答案

我需要像这样为 stringData 添加&符号,而不是仅仅放置&符号.

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

这解决了我的问题.

这篇关于网络连接丢失 NSURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆