Iphone HttpPost失败。我只需要发送3个键/值对

Iphone HttpPost失败。我只需要发送3个键/值对,iphone,objective-c,http-post,Iphone,Objective C,Http Post,发送此http帖子时遇到问题 键值对是Sting和变量 我相信它是当我连接字符串和变量,但我不确定。我尝试使用NSURLDownload执行httpGet,但失败了。如果我知道怎么做,我仍然愿意做“得到”。这是首次使用应用程序时的一次性操作。那么如何将键值对发送回我的网站呢 我没有错误。我只是没有收到任何数据 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex (NSInteger)buttonIndex {

发送此http帖子时遇到问题

键值对是Sting和变量

我相信它是当我连接字符串和变量,但我不确定。我尝试使用NSURLDownload执行httpGet,但失败了。如果我知道怎么做,我仍然愿意做“得到”。这是首次使用应用程序时的一次性操作。那么如何将键值对发送回我的网站呢

我没有错误。我只是没有收到任何数据

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex (NSInteger)buttonIndex
{
NSString *strProvider;
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
   strProvider =@"AT&T";

}
if (buttonIndex == 1)
{
    strProvider =@"Verizon";
}
NSUserDefaults *gPhone =[NSUserDefaults standardUserDefaults];
NSString *myString=[gPhone stringForKey:@"phoneNumber"];


NSString *post = [NSString stringWithFormat:@"phone=",myString,@"&carrier=",strProvider,@"&appid=1"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://rohrwebsolutions.com/api/newuser.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
}

没有检查代码的每一行,但您的变量字符串构造不正确。
stringWithFormat
方法希望将格式用作第一个参数,并将数据替换为其余参数

因此,不是:

NSString *post = [NSString stringWithFormat:@"phone=",myString,@"&carrier=",strProvider,@"&appid=1"];
你想要:

NSString * post = [NSString stringWithFormat:@"phone=%@&carrier=%@&appId=1", myString, strProvider];
另一方面,在我看来,您正在混合使用GET和POST方法来调用URL请求。以代码中显示的方式创建POST数据是非常罕见的


编辑

好的,根据您的评论和shawnwall的回答,显然您需要一些示例代码来进行URL请求。网上有大量的例子,下面是我从一个项目中提取的一个简短片段。它与指定的URL建立同步(阻止)连接以检索数据:

NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: self.url]];
NSURLResponse * response = nil;
NSError * err = nil;

NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse: &response error: &err];
if(err != nil) {
    printf("Error while retrieving URL contents: %s\n", [[err description] UTF8String]);
} else {
    printf("Got %d bytes from server\n", [data length]);
}

用适当的url字符串替换
self.url
(将变量字符串附加到基本url中)。

没有检查代码的每一行,但您的变量字符串构造不正确。
stringWithFormat
方法希望将格式用作第一个参数,并将数据替换为其余参数

因此,不是:

NSString *post = [NSString stringWithFormat:@"phone=",myString,@"&carrier=",strProvider,@"&appid=1"];
你想要:

NSString * post = [NSString stringWithFormat:@"phone=%@&carrier=%@&appId=1", myString, strProvider];
另一方面,在我看来,您正在混合使用GET和POST方法来调用URL请求。以代码中显示的方式创建POST数据是非常罕见的


编辑

好的,根据您的评论和shawnwall的回答,显然您需要一些示例代码来进行URL请求。网上有大量的例子,下面是我从一个项目中提取的一个简短片段。它与指定的URL建立同步(阻止)连接以检索数据:

NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: self.url]];
NSURLResponse * response = nil;
NSError * err = nil;

NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse: &response error: &err];
if(err != nil) {
    printf("Error while retrieving URL contents: %s\n", [[err description] UTF8String]);
} else {
    printf("Got %d bytes from server\n", [data length]);
}

用适当的url字符串替换
self.url
(将变量字符串附加到基本url中)。

您正在发出请求,但实际上并未使用它。您需要将其与NSURLConnection一起使用,下面是一个简单的同步版本:

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
本文中有一些关于异步的有价值的提示:


您正在发出请求,但实际上并未使用它。您需要将其与NSURLConnection一起使用,下面是一个简单的同步版本:

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
本文中有一些关于异步的有价值的提示:


我修复了变量字符串。但它仍然不起作用。我找到的3个post方法显示了这个方法。如果它做得更好的话,你能告诉我那个方向吗。在我的黑莓版本的这个应用程序中,我只使用xmlhttpGet并将值添加到字符串的末尾。我可以这样做吗?如果可以,怎么做?我修复了变量字符串。但它仍然不起作用。我找到的3个post方法显示了这个方法。如果它做得更好的话,你能告诉我那个方向吗。在我的黑莓版本的这个应用程序中,我只使用xmlhttpGet并将值添加到字符串的末尾。我可以这样做吗?如果可以,怎么做?请原谅我的“无趣”。所以我把这个加在最后?此代码中的请求是否为NSMutableURLRequest*请求中的请求对象=[[[NSMutableURLRequest alloc]init]autorelease];?请原谅我的“无礼”。所以我把这个加在最后?此代码中的请求是否为NSMutableURLRequest*请求中的请求对象=[[[NSMutableURLRequest alloc]init]autorelease];?