从iphone app向服务器发布数据

时间:2013-03-20 07:36:53

标签: iphone objective-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];

3 个答案:

答案 0 :(得分: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);

希望这会有所帮助......

祝你好运!!

答案 1 :(得分:0)

我得到了问题的解决方案实际上有他们的无效地址的变量分配问题,这就是为什么它给出访问错误我直接分配值然后它对我很好用

  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];

答案 2 :(得分: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*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);
}

此代码可以解决您的问题。