2013-09-24 88 views
2

我正在從iPhone應用程序發送數據到服務器,但是當我提交表單應用程序crash.Please檢查代碼,並建議我提前致謝。在我的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=%@&[email protected]&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]; 
} 

}

+0

其中是要檢查的代碼? –

+0

在這裏添加代碼來檢查 – manujmv

+0

您可以發佈代碼嗎? – SRI

回答

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; 

    } 
} 

希望這個答案對你有用。您可以通過簡單地更改主體來增加/減少參數數量

+0

感謝Gaurav rastogi .... –

+0

看起來,多部分主體的構造不正確:它以邊界分隔符開始,然後繼續使用邊界分隔符和主體部分。所以,你有兩個邊界分隔符依次。我懷疑這是通過服務器。 ;)在不同的地方,CRLF也是不正確的,但這可能不會導致服務器錯誤。 – CouchDeveloper

+0

更正:兩個連續的邊界分隔符實際上會傳遞服務器:身體部分完全爲空是有效的。儘管如此,我會解決它;) – CouchDeveloper

1

請添加代碼來檢查。

在iOS應用程序,我建議使用AFNetworking

任何服務器 - 客戶端通信。

相關問題