2013-03-20 3 views
-1

Я отправляю данные на сервер из приложения iphone, но он дает исключение при чтении строки сообщения о «избыточном плохом доступе». Тот же код, который я использую для отправки четырех переменных данных, тогда он работает нормально, если Я добавляю больше переменных в сообщение, это дает ошибку.отправка данных на сервер из приложения iphone

NSString*category=titleCategory; 
NSString*sub_Category=titleSubCategory; 
NSString*[email protected]"Audio"; 

content_Title=TitleTextField.text; 
NSString*content_Title=content_Title; 
NSString*[email protected]"Celeritas"; 
    content_Description=descriptionTextField.text; 
NSString*content_Description=content_Description; 

NSString*[email protected]"10"; 
NSString*[email protected]"http://www.celeritas-solutions.com/pah_brd_v1/productivo/productivo/pro/ali.wav"; 


NSString *post =[[NSString alloc] initWithFormat:@"category=%@&sub_Category=%@&content_Type=%@&content_Title=%@&publisher=%@&content_Description=%@&content_ID=%@&content_Source=%@",category,sub_Category,content_Type,content_Title,publisher,content_Description,content_ID,content_Source]; 


NSURL *url=[NSURL URLWithString:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/addData.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); 

Ниже строка, где он ломает код

NSString *post =[[NSString alloc] initWithFormat:@"category=%@&sub_Category=%@&content_Type=%@&content_Title=%@&publisher=%@&content_Description=%@&content_ID=%@&content_Source=%@",category,sub_Category,content_Type,content_Title,publisher,content_Description,content_ID,content_Source]; 
+1

Упомяните строку в коде, где вы получаете ERROR. – Bhavin

+0

... и характер ошибки. (например, журнал ошибок) – Groot

+0

@Vin я добавил строку с ошибкой, пожалуйста, проверьте –

ответ

1
Try this 


    NSString *args = @"category=%@&sub_Category=%@&content_Type=%@&content_Title=%@&publisher=%@&content_Description=%@&content_ID=%@&content_Source=%@"; 

    NSString *values=[NSString stringWithFormat:args,category,sub_Category,content_Type,content_Title,publisher,content_Description,content_ID,content_Source]; 


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

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

    NSString *path = [[NSString alloc] initWithFormat:@"%s",your urlpath]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:path]]; 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody:[values dataUsingEncoding:NSISOLatin1StringEncoding]]; 
    [request setHTTPBody:postData]; 

    NSURLResponse *response; 
    NSError *err; 

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request 
               returningResponse:&response error:&err]; 

    NSString *returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

    [NSURLConnection connectionWithRequest:request delegate:self]; 

    //NSLog(@"String==> %@",returnString); 

Надеется, что это помогает ...

Удачи !!

0

Я получил решение вопроса на самом деле была переменная проблема присвоения их invailded-адрес, поэтому он дает доступ к плохой ошибки я присваиваться значения непосредственно тогда он работал для меня прекрасно, как

NSString*[email protected]"Category"; 
NSString*[email protected]"Working"; 
NSString*[email protected]"Audio"; 

NSString*[email protected]"Content Title"; 
NSString*[email protected]"Celeritas"; 
NSString*[email protected]"ContentDescription"; 




NSString*[email protected]"10"; 
NSString*[email protected]"http://www.celeritas-solutions.com/pah_brd_v1/productivo/productivo/pro/ali.wav"; 


NSString *post =[[NSString alloc] init]; 


post = [NSString stringWithFormat:@"category=%@&sub_Category=%@&content_Type=%@&content_Title=%@&publisher=%@&content_Description=%@&content_ID=%@&content_Source=%@",category,sub_Category,content_Type,content_Title,publisher,content_Description,content_ID,content_Source]; 


NSURL *url=[NSURL URLWithString:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/addData.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]; 
0

После кода справки много в то время как я проверить в проект, а также в .h файле

#import <UIKit/UIKit.h> 


@interface ViewController : UIViewController{ 
    NSURLConnection *connection; 
    NSMutableData *responseData; 
} 

@end 

In.m Файл

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 


    NSString*[email protected]"Cat1"; 
    NSString*[email protected]"Title"; 
    NSString*[email protected]"Audio"; 

    NSString* [email protected]"Test"; 

    NSString*[email protected]"Celeritas"; 

    NSString*[email protected]"Content Description"; 

    NSString*[email protected]"10"; 
    NSString*[email protected]"http://www.celeritas-solutions.com/pah_brd_v1/productivo/productivo/pro/ali.wav"; 


    NSString*post = [NSString stringWithFormat:@"category=%@&sub_Category=%@&content_Type=%@&content_Title=%@&publisher=%@&content_Description=%@&content_ID=%@&content_Source=%@",category,sub_Category,content_Type,content_Title,publisher,content_Description,content_ID,content_Source]; 

    NSURL *url=[NSURL URLWithString:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/addData.php"]; 

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

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


    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60]; 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 



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

#pragma NSUrlConnection Delegate Methods 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    [responseData setLength: 0]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [responseData appendData:data]; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
// [delegate APIResponseArrived:NULL]; 

} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSString *responseString =[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
// [delegate APIResponseArrived:responseString ]; 
    NSLog(@"%@",responseString); 
} 

Этот код решает проблему.

Смежные вопросы