IOS - 相同的ASIHTTPRequest代码不能只使用一种方法

时间:2013-12-04 12:29:05

标签: ios objective-c json asihttprequest

在我的项目中,我使用此代码使其他功能正常工作。但对于这种方法,我无法使其发挥作用。我为不同的方法复制粘贴所有这些代码(更改URL,结果)。此方法的参数仅为int和string。

我总是从SOAP UI和.NET项目中纠正wcf方法。它工作正常。 我只是不能让这个代码适用于这个方法。

[request responseString]会返回患者的信息。请帮我。谢谢。

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:HASTAARAIP]];

    NSData *myPostData = [[NSString stringWithFormat:@"{\"kat\":0,\"oda\":0,\"hastaAdi\":\"KAI\",\"protokolNo\":0,\"yatanAyaktan\":1}"] dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableData *myMutablePostData = [[NSMutableData alloc]initWithData:myPostData];

    [request setPostBody:myMutablePostData];
    [request setRequestMethod:@"GET"];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request setDelegate:self];
    [request startSynchronous];

以下是一些可以让你理解的事情。

使用此代码,[request responseString][request responseStatusMessage]为空。但是,如果我删除addRequestHeader所在的行,则表示对于responseStatusMessage HTML / 1.1HTTP / 1.1 404 Not Found 404.我不明白为什么它会给出404错误。此请求类型适用于其他方法。 ıp就像http://.../rest/<methodname>

为respontring`的一部分添加了日志:

  

服务器在处理请求时遇到错误。例外   消息是'传入消息具有意外的消息格式   '生的'。该操作的预期消息格式是'Xml';   'Json的'。这可能是因为WebContentTypeMapper还没有   在绑定上配置。请参阅文档   WebContentTypeMapper获取更多详细信息。'。请参阅服务器日志了解更多   细节。异常堆栈跟踪是:

更新

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:HASTAARAIP]];
    [request setPostValue:@"0" forKey:@"kat"];
        [request setPostValue:@"0" forKey:@"oda"];
        [request setPostValue:@"KAI" forKey:@"hastaAdi"];
        [request setPostValue:@"0" forKey:@"protokolNo"];
        [request setPostValue:@"1" forKey:@"yatanAyaktan"];
    [request startSynchronous];

但是对于responseString日志:

The server encountered an error processing the request. The exception message is 'The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml'; 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is: 

尝试使用AFNetworking:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"kat": @"0",@"oda": @"0", @"hastaAdi":hastaAdi, @"protokolNo":@"0",@"yatanAyaktan":@"1"};
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    [manager POST:HASTAARAIP parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

但是日志是这样的:

Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8e49ce0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

1 个答案:

答案 0 :(得分:1)

ASI早已被开发者抛弃了。使用AFNetworking 2.0会更容易,并且有很多好的文档:https://github.com/AFNetworking/AFNetworking

看起来您正在尝试发出@“POST”请求。 您可以使用以下代码段(在类中使用IOS构建)来发出请求:

 NSDictionary *parameters = @{@"key" : value};
 /* the following serialization is NOT a deep serialization */
 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil];

        NSURL *url = [NSURL URLWithString:@"http://www.someURL.com/whatEver"];

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0f];

        [request setHTTPMethod:@"POST"];

        [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

        [request setHTTPBody:jsonData];

        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

            if ([response isKindOfClass:[NSHTTPURLResponse class]]) {

                NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

                NSLog(@"all headers from response:%@", [httpResponse allHeaderFields]);
                NSLog(@"status code from response:%ld", (long)[httpResponse statusCode]);
            }
        }];