嗨我是ios中的新手,我今天没有发送任何调用,直到现在我已经尝试过以下代码
-(void)sendRequest
{
NSString *vali = @"$uppl!3r$";
NSString *post = [NSString stringWithFormat:@"key1=%@",vali];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"%@",postLength);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.ddemo3.enerjinet.com/webservices/ios/suppliers.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
webData = [[NSMutableData data] retain];
NSLog(@"%@",webData);
}
else
{
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length: [webData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",loginStatus);
//greeting.text = loginStatus;
[loginStatus release];
[connection release];
[webData release];
}
它应该返回我76个记录的数组,但它返回给我<>
任何人都可以帮助我吗? Web服务已经准备就绪我需要获取数组作为响应并在我的表视图中显示它请帮助我这样做
答案 0 :(得分:0)
使用:
+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
除非你有令人信服的理由不这样做。
示例(可能不工作):
注意创建postData
。
-(void)sendRequest {
NSString *vali = @"$uppl!3r$";
NSString *post = [NSString stringWithFormat:@"key1=%@",vali];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%lu", [postData length]];
NSLog(@"%@", postLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.ddemo3.enerjinet.com/webservices/ios/suppliers.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"data: %@", data);
}];
}
哦,让生活更轻松,使用ARC
。
答案 1 :(得分:0)
有几点想法:
您NSLog
webData
,它将始终显示<>
(因为您在实例化后立即记录它)。我不确定你为什么记录它。
问题是,您是<>
还是NSLog
中的connectionDidFinishLoading
。
我问这是因为你没有在error
中记录connection:didFailWithError:
,如果失败,你永远不会知道原因。你真的应该在error
中记录connection:didFailWithError:
,以便知道它是否失败,如果是,为什么:
NSLog(@"%s: %@", __FUNCTION__, error);
在connection:didReceiveResponse:
中,你真的应该看看HTTP status code:
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200)
NSLog(@"%s: status code is %d; should be 200", __FUNCTION__, statusCode);
}
如果不是200,你真的想知道这件事。
您在其中一条评论中报告您看到connectionDidFinishLoading:
被叫,但从未拨过didReceiveData
。这意味着(毫不奇怪)没有收到数据。所以,你应该:
确认connection:didReceiveResponse:
报告了statusCode
200
;以及
确认服务器代码正常运行。如果你的服务器PHP代码出错,我可以想象得到你描述的行为(由于服务器通常在display_errors
文件中关闭php.ini
而加剧了这种情况。
顺便说一句,如果与key1
关联的值可能包含任何保留字符(如RFC 3986的section 2中所定义),则应使用{{1}对字符串进行百分比转义}}。因此:
CFURLCreateStringByAddingPercentEscapes
根据NSString *post = [NSString stringWithFormat:@"key1=%@", [self percentEscapeString:vali]];
的{{3}},您不仅可以转义百分比,还可以用application/x-www-form-urlencoded
个字符替换空格,因此:
+