问题描述
您好,我正在开发一个应用程序,该应用程序可为您提供有关您当前位置(国家,城镇,街道)的信息,因此我想将城镇和街道发送到本地Web服务器!我发现使用此URL时,技术上可行的代码:
Hi im working on a application that gives you information about your current location(Country, town,street) so I want to send the town and street to a local web server ! i found a code that technically works when i use this URL :
@"http://localhost:8888/jml/save.php?town=California&street=whatever"
但是我想要的是从标签(townText/streetText)中获取值,此时似乎什么都没有??
But what i want is to get the values from a labels (townText/ streetText) and at this point nothing seems to happing ?!!
这是代码:
if (![self.townText.text isEqualToString:@""] && ![self.streetText.text isEqualToString:@""]) {
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat**:@"http://localhost:8888/jml/save.php?town=%@&street=%@",self.townText.text,streetText.txt**]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSData *urlData;
NSURLResponse *response;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
所以请您帮忙:)谢谢!
So please i need your help :) and thank you !
推荐答案
您的iPhone本身未运行Web服务器-它正在开发计算机上运行.尝试将"localhost"切换为机器的IP地址,并确保可以从开发电话所在的任何网络上访问该机器.
Your iPhone isn't running the webserver itself - it's running on your development machine. Try switching "localhost" for your machine's IP address, and make sure that it's reachable from whatever network your development phone is on.
此外-确保您转义了要输入URL的字段.如果它们包含在URL中无效的字符,则您的请求将失败.您可以像这样转义您的URL:
In addition - make sure that you're escaping the fields you're putting in the URL. If they contain characters that are not valid in a URL, your request will fail. You can escape your URL like so:
NSString* myURLString = [NSString
stringWithFormat:@"http://localhost:8888/jml/save.php?town=%@&street=%@", self.townText.text, self.streetText.text];
myURLString = [myURLString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
也:我假设您的代码示例中的那些双星号仅用于说明,因为这不是有效的Obj-C语法.您还引用了不存在的成员( streetText.txt
)-您可能输入了错误的 streetText.text
.
Also: I'm assuming that those double-asterisks in your code sample are for illustration only, as that's not valid Obj-C syntax. You're also referencing a member (streetText.txt
) that doesn't exist - you may have mistyped streetText.text
.
这篇关于将GET HTTP请求从iPhone发送到本地Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!