[英]How to send data from iphone to server using the url
我想知道如何使用URL从iphone应用程序将数据发送到mysql服务器中的表。
http://localhost/location.php?id =&latitude = @&longitude = @
其中“ @”是xcode中的纬度和经度变量。
嗨,朋友,您需要首先使用ASIHttp Connection Delegate设置连接
以这种方式与服务器交互的最常见方法是通过NSString stringWithContentsOfURL:encoding:error:
方法(以及NSString中的其他相关方法)和NSURLConnection类,它们能够执行异步后台请求。
以上两个链接的类参考文档都包含代码示例(请参阅每个示例顶部的“相关示例代码”部分)。
此外,还有第三方解决方案可用,例如常用的ASIHTTPRequest类。
数据“命中”服务器后,您需要先从$ _GET超全局变量中检索数据,然后再将其存储在数据库表中。 有很多种方法(使用PDO等抽象层),因此它实际上取决于您的个人喜好。
您可以使用以下代码首先,您可能需要导入asihttprequest包和所有必需的框架。
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
在您的viewcontroller.h文件中
ASINetworkQueue *networkQueue;
在您的viewController.m文件中:-在viewdidLoad中:-
networkQueue=[[ASINetworkQueue queue]retain];
在要发送请求的位置编写以下代码:-
NSString *str = [NSString stringWithFormat:@"http://localhost/location.php?id=&latitude=%@&longitude=%@",latitude,longitude];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setRequestMethod:@"GET"];
[request setDelegate:self];
[request setDidFinishSelector: @selector(*writemethodforsuccessfulrequest*)];
[request setDidFailSelector: @selector(*writemethodforrequestfailed*)];
[networkQueue addOperation: request];
[networkQueue go];
在dealloc中:-
[networkQueue cancelAllOperations];
[networkQueue release];
简单尝试一下
NSString *urlString = [NSString stringWithFormat:@"http://localhost/location.php?id=&latitude=%@&longitude=%@",yourLat,yourLong];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *returnData = [NSData dataWithContentsOfURL:url];
// returnData will be the response you get when you pass the URL. If you like to
// get some acknowledgement pass a xml format with some parameters to check
// whether the URL you sent is valid one.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.