简体   繁体   English

如何使用URL将数据从iPhone发送到服务器

[英]How to send data from iphone to server using the url

I wish to know how i can send data from my iphone app to a table in mysql server using the url. 我想知道如何使用URL从iphone应用程序将数据发送到mysql服务器中的表。

http://localhost/location.php?id=&latitude=@&longitude=@ http://localhost/location.php?id =&latitude = @&longitude = @

where the "@" are the latitude and longitude variables from xcode. 其中“ @”是xcode中的纬度和经度变量。

嗨,朋友,您需要首先使用ASIHttp Connection Delegate设置连接

The most common means of interacting with a server in this manner are via the NSString stringWithContentsOfURL:encoding:error: method (and other related methods in NSString) and the NSURLConnection class, which is capable of performing asynchronous background requests. 以这种方式与服务器交互的最常见方法是通过NSString stringWithContentsOfURL:encoding:error:方法(以及NSString中的其他相关方法)和NSURLConnection类,它们能够执行异步后台请求。

Both of the above linked class reference documents contain code samples (see the "Related sample code" section at the top of each). 以上两个链接的类参考文档都包含代码示例(请参阅每个示例顶部的“相关示例代码”部分)。

Additionally, there are 3rd party solutions available, such as the commonly used ASIHTTPRequest class. 此外,还有第三方解决方案可用,例如常用的ASIHTTPRequest类。

Once the data "hits" the server, you'd then retrieve the data from the $_GET superglobal prior to storing it in the database table. 数据“命中”服务器后,您需要先从$ _GET超全局变量中检索数据,然后再将其存储在数据库表中。 There are a wide variety of approaches to this (using an abstraction layer such as PDO , etc.) so it really depends on your personal preference. 有很多种方法(使用PDO等抽象层),因此它实际上取决于您的个人喜好。

You can use the following code First you might have import asihttprequest package and all the required frameworks.after that 您可以使用以下代码首先,您可能需要导入asihttprequest包和所有必需的框架。

#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"

In your viewcontroller.h file 在您的viewcontroller.h文件中

ASINetworkQueue  *networkQueue;

In your viewController.m file:- in viewdidLoad:- 在您的viewController.m文件中:-在viewdidLoad中:-

networkQueue=[[ASINetworkQueue queue]retain];

Write the following code where you want to send the request:- 在要发送请求的位置编写以下代码:-

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

In dealloc:- 在dealloc中:-

[networkQueue cancelAllOperations];
[networkQueue release]; 

Simple Try this 简单尝试一下

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM