簡體   English   中英

如何使用URL將數據從iPhone發送到服務器

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM