2

我正在将数据从 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];
}

}

4

2 回答 2

1

请添加代码进行检查。

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

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

于 2013-09-24T06:11:00.567 回答
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;

    }  
}

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

于 2013-09-24T06:26:01.193 回答