简体   繁体   中英

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.

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

where the "@" are the latitude and longitude variables from 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.

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.

Once the data "hits" the server, you'd then retrieve the data from the $_GET superglobal prior to storing it in the database table. 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.

You can use the following code First you might have import asihttprequest package and all the required frameworks.after that

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

In your viewcontroller.h file

ASINetworkQueue  *networkQueue;

In your viewController.m file:- in 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:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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