使用NSDictionary的POST请求

时间:2013-11-22 11:16:25

标签: php html ios objective-c nsdictionary

嗨大家好:D请向我解释我应该如何在你的课堂上使用NSDictionary(关于page)。我该说什么request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; that my program work correctly?

这是我的代码

#import "MainClass.h"

@implementation MainClass

- (IBAction)actionButton:(id)sender {

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL
                                                                    URLWithString:@"https://login.dnevnik.ru"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];


request.HTTPMethod = @"POST";
NSString *username = _loginLogin.text;
NSString *password= _passwordLogin.text;
GlobalUserName = username;
GlobalPassword = password;
NSDictionary *param = @{@"Username": GlobalUserName,@"password": GlobalPassword};



/*NSUserDefaults *loginData = [NSUserDefaults standardUserDefaults];
NSString *username1 = [loginData objectForKey:@"username"] ;
NSString *password1 = [loginData objectForKey:@"password"];*/


//Параметры посылаемого запроса
request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:@"application/x-www-form-urlencoded;charset=UTF-8"
forHTTPHeaderField:@"Content-Type"];


NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


if (connection) {
    _otvet.text = @"Соединение установлено";
    NSLog(@"ama ama");

}
else
{
    _otvet.text=@"Проблема с соединением";
    NSLog(@"ama ama faza");
}

}

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

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"Ошибка" message:@"Пожалуйста, проверьте
соединение с Интернетом." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];          
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSString * data = [[NSString alloc] initWithData:receivedData    
encoding:NSUTF8StringEncoding];

if ([data   isEqualToString: @"OK"]) {

    _otvet.text = @"Вы успешно залогинены.";
    NSLog(@"Вы успешно залогинены.");
}
else
{
    NSLog(@"Ошибка авторизации %@,%@",GlobalUserName,GlobalPassword);
}
}

在我的情况下,xcode在编译时说

  

'NSDictionary'没有可见的@interface声明选择器   'dataUsingEncoding:'

1 个答案:

答案 0 :(得分:2)

您应该将NSString作为BODY传递。

这样做:

NSString *postString = [NSString stringWithFormat:@"Username=%@&password=%@",username,password];
request.HTTPBody = [postString dataUsingEncoding:NSUTF8StringEncoding];

或者,如果你想将NSDictionary编码为JSON字符串并在后端解码它 - 更好的方法,但更多的工作:)

request.HTTPBody = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:&error]