IOS - Same ASIHTTPRequest code doesn't work only one method

535 Views Asked by At

In my project I make the other functions work with this code. But for this method I cannot make it work. I did copy-paste all this code for different methods(changing URL ,Result). The parameters of this methods are only int and string.

I always correct the wcf method from SOAP UI and .NET project.It works correct. I only cannot make this code work for this method.

The [request responseString] returns a patient's information. Please help me. Thank you.

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

Here is some things that can make you understand.

With this code the [request responseString] and [request responseStatusMessage] is null. But if I remove the line where the addRequestHeader, it says for responseStatusMessage HTML/1.1HTTP/1.1 404 Not Found 404. I didn't understand why it gives this 404 error. This request type works for the other methods. The ıp is like http://.../rest/<methodname>

Added log for a part of 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:

Updated:

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

But for responseString the logs:

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: 

Trying with 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);
    }];

But the log is like this:

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

There are 1 best solutions below

1
On

ASI was long abandoned by the developer. using AFNetworking 2.0 will be more easy, and with a lot of good documentation: https://github.com/AFNetworking/AFNetworking

it looks like you are trying to make a @"POST" request. you can use the following code snippet (uses IOS build in classes) to make the request:

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