Php 如何在ios上从web服务器加载数据
我一直在研究如何将数据从mysql服务器加载到ios设备。我看到的大多数示例都将变量发送到服务器(如用户名和密码),然后使用POST检索所需信息。在我的情况下,我不需要向服务器发送任何信息,只需检索即可 所以我的问题是,我需要使用POST来获取信息吗 此外,我似乎找不到php部分的任何示例。我应该如何编写php文件以返回数据库表中的所有信息?抱歉,这听起来像一个模糊的问题,但我似乎找不到任何例子Php 如何在ios上从web服务器加载数据,php,ios,webserver,Php,Ios,Webserver,我一直在研究如何将数据从mysql服务器加载到ios设备。我看到的大多数示例都将变量发送到服务器(如用户名和密码),然后使用POST检索所需信息。在我的情况下,我不需要向服务器发送任何信息,只需检索即可 所以我的问题是,我需要使用POST来获取信息吗 此外,我似乎找不到php部分的任何示例。我应该如何编写php文件以返回数据库表中的所有信息?抱歉,这听起来像一个模糊的问题,但我似乎找不到任何例子 非常感谢任何能帮忙的人 您可以使用以下内容: - (void)viewDidLoad {
非常感谢任何能帮忙的人 您可以使用以下内容:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSURL *url = [NSURL URLWithString:__YOUR URL__];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (connection) {
self.rssData = [NSMutableData data];
} else {
NSLog(@"Connection failed");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.rssData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.rssData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSString *result = [[NSString alloc] initWithData:self.rssData encoding:NSUTF8StringEncoding];
NSLog(@"%@",result);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"%@", error);
}
希望能有帮助 您还可以使用GET检索数据 例如: In.h
非常感谢!你有php方面的例子吗?我似乎不知道该怎么写。
NSURL*url=[NSURL URLWithString:@”http://php.net/downloads.php"];代码>很好,谢谢,但是我应该以什么方式编写我的php方法呢?我写了一个查询来获取数据,之后我该怎么做?我必须为POST分配每个变量吗?比如$u POST[“book\u name”]?
@interface WSHandler : NSObject <NSURLConnectionDelegate>
{
NSMutableData *receivedData;
}
- (void)callWebService;
@end
- (void)callWebService
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:WS_URL]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection)
{
receivedData = nil;
}
else
{
DLog(@"Connection could not be established");
}
}
#pragma mark - NSURLConnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
if (!receivedData)
receivedData = [[NSMutableData alloc] initWithData:data];
else
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
DLog(@"***** Connection failed");
receivedData=nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
DLog(@"***** Succeeded! Received %d bytes of data",[receivedData length]);
DLog(@"***** AS UTF8:%@",[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]);
}