2013-03-20 70 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

請在代碼中提及該行,您在哪裏得到錯誤。 – Bhavin 2013-03-20 07:41:02

+0

...和錯誤的性質。 (例如錯誤日誌) – Groot 2013-03-20 07:41:28

+0

@Vin我已經添加了錯誤行,請檢查 – 2013-03-20 07:47:11

回答

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

我得到了問題的解決實際上有對,這就是爲什麼它被賦予訪問糟糕的錯誤,我指派值直接,然後它的工作對我罰款像

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

下面的代碼的invailded地址變量賦值問題一個有很大的幫助,而我考到項目以及 在.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); 
} 

此代碼解決您的問題。

相關問題