从iphone应用程序向服务器发布数据

从iphone应用程序向服务器发布数据,iphone,objective-c,Iphone,Objective C,我正在从iphone应用程序向服务器发布数据,但在读取关于“过度错误访问”的帖子行代码时出现异常。我用于发送四个变量数据的代码与此相同,如果我在帖子中添加更多变量,则工作正常。这会导致错误 NSString*category=titleCategory; NSString*sub_Category=titleSubCategory; NSString*content_Type=@"Audio"; content_Title=TitleTextField.text; NSString*c

我正在从iphone应用程序向服务器发布数据,但在读取关于“过度错误访问”的帖子行代码时出现异常。我用于发送四个变量数据的代码与此相同,如果我在帖子中添加更多变量,则工作正常。这会导致错误

    NSString*category=titleCategory;
NSString*sub_Category=titleSubCategory;
NSString*content_Type=@"Audio";

content_Title=TitleTextField.text;
NSString*content_Title=content_Title;
NSString*publisher=@"Celeritas";
    content_Description=descriptionTextField.text;
NSString*content_Description=content_Description;

NSString*content_ID=@"10";
NSString*content_Source=@"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];
希望这有助于


祝你好运

我得到了问题的解决方案,实际上,他们入侵的地址存在变量分配问题,这就是为什么它给了访问权限错误,我直接赋值,然后它对我有效,就像

  NSString*category=@"Category";
NSString*sub_Category=@"Working";
NSString*content_Type=@"Audio";

NSString*content_Title=@"Content Title";
NSString*publisher=@"Celeritas";
NSString*content_Description=@"ContentDescription";




NSString*content_ID=@"10";
NSString*content_Source=@"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];

当我在项目中进行测试时,下面的代码会有很大帮助 在.h文件中

#import <UIKit/UIKit.h>


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

@end

此代码解决了您的问题。

请提及代码中的行,您在哪里得到错误…以及错误的性质。(例如错误日志)@Vin我已添加错误行,请check@DeveloperIOS:您是否尝试过拆分初始化和分配“post”。@Vin是,但错误相同
#import <UIKit/UIKit.h>


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

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


    NSString*category=@"Cat1";
    NSString*sub_Category=@"Title";
    NSString*content_Type=@"Audio";

    NSString* content_Title=@"Test";

    NSString*publisher=@"Celeritas";

    NSString*content_Description=@"Content Description";

    NSString*content_ID=@"10";
    NSString*content_Source=@"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);
}