【问题标题】:POST Request with NSDictionary使用 NSDictionary 发布请求
【发布时间】:2013-11-22 11:16:25
【问题描述】:

嗨,伙计们: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:'

【问题讨论】:

  • 我知道这是旧的。但希望你能回应。你在 Mainclass.h 中定义了什么 GlobalUserName 和 GlobalPassword

标签: php html ios objective-c nsdictionary


【解决方案1】:

您应该将 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]

【讨论】:

  • 应该如...不能通过nsdictionary?因为我在将 NSDictionary 传递给 php 脚本时遇到问题... NSStrings 工作但不是 NSDictionary
  • 你可以,只需使用:request.HTTPBody = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 2015-02-16
  • 2013-02-22
  • 2015-05-08
相关资源
最近更新 更多