0

这里的错误 这是邮递员客户端的屏幕截图。该 api 正在处理我的代码中的问题,如何设置响应和请求。我正在使用 AFNetworking 3.0 在目标 c 中发出发布请求,但请求响应错误请求(400)这是错误

{ status code: 400, headers {
    "Cache-Control" = private;
    "Content-Length" = 1647;
    "Content-Type" = "text/html";
    Date = "Sat, 09 Apr 2016 15:49:32 GMT";
    Server = "Microsoft-IIS/7.5";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} }, NSErrorFailingURLKey=http://mytesturl_Json.svc/ValidateLoginService, com.alamofire.serialization.response.error.data=<efbbbf3c 3f786d6c

这是发出帖子请求的代码

 AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

 manager.requestSerializer = [AFJSONRequestSerializer serializer];
 manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

    [manager POST:@"http://mytesturl_Json.svc/ValidateLoginService" parameters:inputs progress:nil success:^(NSURLSessionTask *task, id responseObject) {

        NSLog(@"JSON: %@", responseObject);

    } failure:^(NSURLSessionTask *operation, NSError *error) {
        NSLog(@"Error: %@", error);

    }];

我也试过这样

-(void)postdata
{
    NSError *error;

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURL *url = [NSURL URLWithString:@"http://virtuzo.in/AhprepaidTestService/AhprepaidAPI_Json.svc/ValidateLoginService"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];

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

    [request setHTTPMethod:@"POST"];
    NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"admin", @"UserName",
                             @"1234", @"Pwd",
                             nil];
    NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
    [request setHTTPBody:postData];


    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        NSLog(@"reponse==%@",response);
        NSLog(@"error==%@",error);


    }];

    [postDataTask resume];

}

我仍然收到同样的错误

reponse==<NSHTTPURLResponse: 0x7f9baa433990> { URL: http://virtuzo.in/AhprepaidTestService/AhprepaidAPI_Json.svc/ValidateLoginService } { status code: 400, headers {
    "Cache-Control" = private;
    "Content-Length" = 1647;
    "Content-Type" = "text/html";
    Date = "Thu, 14 Apr 2016 02:57:21 GMT";
    Server = "Microsoft-IIS/7.5";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} }
2016-04-14 08:27:22.833 Ahprepaid[79163:1281702] error==(null)
4

2 回答 2

0

原因是您获得的 Web 服务响应实际上不是 JSON 响应。检查您收到的响应,这将有助于您的代码工作!

于 2016-04-09T16:01:15.270 回答
0

尝试不同的响应序列化程序。响应格式是什么?它是字符串还是 json 格式?根据该设置响应序列化程序。您可以在不同的工具中检查您的回复,例如提前休息客户端、邮递员,或者您可以询问制作 api 或 web 服务的人..!

更新 :

Update :

NSError *error;

 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  NSURL *url = [NSURL URLWithString:@"[JSON SERVER"];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                  cachePolicy:NSURLRequestUseProtocolCachePolicy
                                           timeoutInterval:60.0];

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

 [request setHTTPMethod:@"POST"];
 NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"TEST   IOS", @"name",
             @"IOS TYPE", @"typemap",
             nil];
 NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
 [request setHTTPBody:postData];


  NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

  }];

  [postDataTask resume];

尝试这样的事情。这是没有网络连接的本地方式。您将在完成处理程序中将数据作为响应。如果您的响应是字符串,则将该数据转换为字符串。如果以 json 格式响应,则在完成处理程序块中以 json 格式转换该数据。转换示例,

 //data to string
  NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

 /data to json object
 NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:myNSData options:kNilOptions error:&error]; // here two posibilitiew NSArray or NSDictionary (Try both ot you can use id)

希望这能解决你的问题.. :)

更新 2:

NSString *urlString2 = @"http://virtuzo.in/AhprepaidTestService/AhprepaidAPI_Json.svc/ValidateLoginService?UserName=admin&Pwd=1234";



NSURL *myUrl = [NSURL URLWithString:urlString2];





NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myUrl];
[request setHTTPMethod:@"POST"];


 NSURLSession *session = [NSURLSession sharedSession];


[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {


    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"str is %@",str);




}]resume];

这与邮递员的响应相同。我已经在我的演示中尝试过。我只是在 url 中附加参数。

更新 3:

您正在获取 xml 字符串,因此序列化并不复杂。

1) 下载XML 解析器。derag 并放入您的项目中。(当你构建时,你会在这个类中得到错误。每当你在这个类中得到错误注释(//)这一行。这是因为它没有使用 ARC)。

更新代码:

 NSString *urlString2 = @"http://virtuzo.in/AhprepaidTestService/AhprepaidAPI_Json.svc/ValidateLoginService?UserName=admin&Pwd=1234";



NSURL *myUrl = [NSURL URLWithString:urlString2];





NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myUrl];
[request setHTTPMethod:@"POST"];


 NSURLSession *session = [NSURLSession sharedSession];


[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {


    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"str is %@",str);




    NSError *parseError = nil;
    NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:str error:&parseError];
    NSLog(@" %@", xmlDictionary);

    NSString *myfinalString = [[xmlDictionary valueForKey:@"string"]valueForKey:@"text"];
    NSLog(@"string is %@",myfinalString);



    NSError *jsonError;
    NSData *objectData = [myfinalString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                         options:NSJSONReadingMutableContainers
                                                           error:&jsonError];
    NSLog(@"json is : %@",json);

}]resume];
于 2016-04-09T18:16:57.470 回答