开发者

HttpPost fail. I just need to sent 3 key/value pairs

开发者 https://www.devze.com 2023-03-30 16:01 出处:网络
Having trouble sending this http post. Key value pairs are stings and VARIABLES I believe its when i concatenate the string and variables, but I\'m not sure.I tried to do a httpGet with NSURLDownloa

Having trouble sending this http post.

Key value pairs are stings and VARIABLES

I believe its when i concatenate the string and variables, but I'm not sure. I tried to do a httpGet with NSURLDownload but failed. I am willing to still do the "Get" if I knew how. This is a one time action when the app is first used. So how do I send key value pairs back to my website?

I get no errors. I just don't receive any data.

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex (NSInteger)buttonIndex
{
NSString *strProvider;
// the user clicked one o开发者_StackOverflow社区f 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];
}


Didn't look through every line of your code, but your are not constructing your variable string correctly. The stringWithFormat method expects the format to be used as the first argument, and the data to be substituted as the remaining arguments.

So instead of:

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

You want:

NSString * post = [NSString stringWithFormat:@"phone=%@&carrier=%@&appId=1", myString, strProvider];

As an aside, it looks to me like you are mixing the GET and POST methods for invoking URL requests. It's very unusual to create POST data in the way you showed in your code.


EDIT

Ok, based on your comment and shawnwall's answer its obvious you need some sample code for making URL requests. There is a ton of examples online and on SO, here is a brief snippet I extracted from one of my projects. It makes a synchronous (blocking) connection to the designated URL to retrieve data:

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

Replace self.url with an appropriate URL string (append your variable string to your base URL).


You are making a request but not actually using it. You need to use it with NSURLConnection, here's a simple synchronous version:

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

There's some valuable tips in this article for asynch:

http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号