在我的iPhone应用程序中的服务器上发布数据

时间:2013-09-24 06:04:27

标签: iphone ios

我正在从iPhone应用程序向服务器发送数据但是当我提交表单应用程序崩溃时。请检查代码并建议我提前建议。

-(IBAction)SAVE:(id)sender
{

NSString *post =[[NSString alloc] initWithFormat:@"amount=50&termid=1&state=%@&city=%@&post_title=%@&description=%@&post_price=10&post_location=asd&geo_latitude=%@&geo_longitude=%@&owner_name=%@&owner_email=abc@a.a&owner_phone=%@&post_tags=a&post_url=%@&coupon_code=a&current_userid=1&type=renew",txt4.text,txt5.text,txt6.text,latValue,longValue,txt7.text,txt8.text,txt9.text,txt10.text];

NSLog(@"String Posted:%@",post);
NSURL *url=[NSURL URLWithString:@"http://www.Example.com.mx/testing/publication.php?"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

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

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",data);

con=[NSURLConnection connectionWithRequest:request delegate:self];
if(con){
    responseData=[NSMutableData data];
}

}

2 个答案:

答案 0 :(得分:1)

请添加要检查的代码。

在iOS应用程序中,我建议使用AFNetworking

用于任何服务器 - 客户端通信。

答案 1 :(得分:1)

-(void)uploadDataToServer
{
     //Server Address URL
     NSString *urlString = [NSString stringWithFormat:@"Your Server Address"];

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
     [request setURL:[NSURL URLWithString:urlString]];
     [request setHTTPMethod:@"POST"];

     NSMutableData *body = [NSMutableData data];

     NSString *boundary = @"---------------------------14737809831466499882746641449";
     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
     [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
     [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    //parameter first
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    //Attaching the key name @"parameter_first" to the post body
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter_first\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    //Attaching the content to be posted ( ParameterFirst )
    [body appendData:[[NSString stringWithFormat:@"%f",ParameterFirst] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    //parameter second
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    //Attaching the key name @"parameter_second" to the post body
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter_second\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    //Attaching the content to be posted ( ParameterSecond )
    [body appendData:[[NSString stringWithFormat:@"%f",ParameterSecond] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    //parameter third
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    //Attaching the key name @"parameter_third" to the post body
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter_third\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
     //Attaching the content to be posted ( ParameterThird )
    [body appendData:[[NSString stringWithFormat:@"%d",ParameterThird] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    //close form
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    NSLog(@"Request  = %@",[[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]);

    //setting the body of the post to the reqeust
    [request setHTTPBody:body];

    //now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"returnString %@",returnString);

    NSDictionary* jsonResponse = [CommonFunctions parseJSON:returnData];
    NSLog(@"Json responce %@",jsonResponse);
    if ([[jsonResponse objectForKey:@"replyCode"] isEqualToString:@"success"])
    {
        isSuccessfulPost=YES;
    }
    else
    {
        isSuccessfulPost=NO;

    }  
}

希望答案对你有用。您可以通过简单地改变身体来增加/减少参数数量