在iPhone中向网络服务器发布数据

时间:2013-09-04 05:46:36

标签: iphone ios objective-c xcode nsurlconnection

我搜索但我无法解决我的问题。我想将数据发布到服务器。我有一个用户默认,textview和textfield。请帮帮忙。

 NSUserDefaults *getting= [NSUserDefaults standardUserDefaults] ;
 userID1=[getting   objectForKey:@"uId"];

 NSString *bodyString=[NSString stringWithFormat:@"userId=%@&question=%@&categoryId=%@",userID1,askTextViewObj.text,specialistField.text];

 [bodyString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

 NSLog(@"%@",bodyString);

request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"XXXXXXXXXXXXXXXXXX" ] cachePolicy:0 timeoutInterval:30];                 
[request setHTTPMethod:@"POST"];
[request setHTTPShouldHandleCookies:YES];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[request addValue:[NSString stringWithFormat:@"%d", [bodyString length]] forHTTPHeaderField:@"content-Length"];
connection=[NSURLConnection connectionWithRequest:request delegate:self ];
if (!connection)
{
    UIAlertView* aler = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Failed to Connect Server " delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
    [aler show];

}

else
{
    NSLog(@"Connection is Successfully");

}

0 个答案:

没有答案