0

我正在尝试使用以下参数调用 Web 服务:

{"Class":"Authorization","method":"login","user":"","pass":""}

如下

NSString *user = [NSString stringWithFormat:@"username=%@",_username.text];
NSString *pass = [NSString stringWithFormat:@"password=%@",_password.text];
NSString *Class = @"Authorization";
NSString *method = @"login";

//NSString *post = [NSString stringWithFormat:@"%@&%@&%@&%@", user, pass, Class, method ];
    
NSString *jsonPostBody = [NSString stringWithFormat:@"'json' = '{\"Class\":"
                              "\"%@\""
                              ",\"method\":"
                              "\"%@\""
                              ",\"pass\":"
                              "\"%@\""
                              ",\"user\":"
                              "\"%@\""
                              "}'",
                              [Class stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                              [method stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                              [pass stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                              [user stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSData *postData = [jsonPostBody dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
   
    NSURL *url = [NSURL URLWithString:@"http://212.119.87.45:8080/IktissabServices/index.php/service"];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:180.0];
    
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded"   forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:[[jsonPostBody stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
                          dataUsingEncoding:NSUTF8StringEncoding
                          allowLossyConversion:YES]];
    
    [request setHTTPBody:postData];
    
    NSString* postDataLengthString = [[NSString alloc] initWithFormat:@"%d", [postData length]];
    [request setValue:postDataLengthString forHTTPHeaderField:@"Content-Length"];
    
    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSError *error;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];//
    if (! error) {
        NSLog(@"%@",jsonDict);
    }else{
        NSLog(@"%@",error.localizedDescription);
    } 

每次结果都是:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“数据参数为零”

网络服务有:

输入内容类型:文本/json

输出内容类型:文本/json

任何帮助或示例将不胜感激。

4

1 回答 1

0

以下代码没有任何异常(尽管显然我无权访问您的服务器,因此我无法对其进行端到端测试):

NSDictionary *dictionary = @{
    @"Class"  : @"Authorization",
    @"method" : @"login",
    @"pass"   : _password.text,
    @"user"   : _username.text
};

NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:dictionary
                                                   options:0
                                                     error:&error];
if (error)
    NSLog(@"Failure to serialize JSON object %@", error);

NSURL *url = [NSURL URLWithString:@"http://212.119.87.45:8080/IktissabServices/index.php/service"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                   timeoutInterval:180.0];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];    
[request setHTTPBody:postData];

NSURLResponse *response;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error)
    NSLog(@"%s sendSynchronousRequest error %@", __FUNCTION__, error);

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];//
if (!error) {
    NSLog(@"%@",jsonDict);
} else {
    NSLog(@"%s JSONObjectWithData error %@", __FUNCTION__, error);
} 

或者,如果您没有使用最新的 Xcode 并且无法访问现代 Objective C 字典的定义,那么您显然可以以传统方式定义字典:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"Authorization", @"Class",
                            @"login",         @"method",
                            _password.text,   @"pass",
                            _username.text,   @"user",
                            nil];

编辑:

从您jsonPostBody的 推断,字典条目可能需要是:

NSDictionary *dictionary = @{
    @"json" : @{
        @"Class"  : @"Authorization",
        @"method" : @"login",
        @"pass"   : _password.text,
        @"user"   : _username.text
    }
};

或者可能(虽然,更不可能),从你的userpass定义推断:

NSDictionary *dictionary = @{
    @"json" : @{
        @"Class"  : @"Authorization",
        @"method" : @"login",
        @"pass"   : [NSString stringWithFormat:@"password=%@",_password.text],
        @"user"   : [NSString stringWithFormat:@"username=%@",_username.text]
    }
};

这是一个如何为您的服务器格式化 JSON 的问题。

于 2012-11-14T14:52:26.680 回答