AFNetworking网络请求常见错误锦集

1. Code=3840


fails Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set.
" UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

原因及解决方法:
1.直接找后台(Php)人员,主要原因在于后台人员在处理前端上传图片之后对图片处理的方法不正确导致。

2. Code=-1016

下面是返回的错误信息:

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo={com.alamofire.serialization.response.error.response= { URL: http://121.40.152.172:8080/sjdw/user/appLogin.do } { status code: 200, headers {
    "Content-Length" = 101;
    Date = "Wed, 01 Jun 2016 03:08:13 GMT";
    Server = "Apache-Coyote/1.1";
} }, NSErrorFailingURLKey=http://121.40.152.172:8080/sjdw/user/appLogin.do, com.alamofire.serialization.response.error.data=<7b227265 73756c74 4d736722 3a227375 63636573 73222c22 696e666f 223a7b22 75736572 4964223a 22386132 38393633 35353530 35626534 37303135 35303632 33393865 33303030 31222c22 75736572 4e616d65 223a2231 33303030 30303030 3030227d 7d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/plain}

解决方法:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];下面添加
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

// manager.responseSerializer= [AFHTTPResponseSerializer serializer];//使用这个将得到的是NSData
//manager.responseSerializer = [AFJSONResponseSerializer serializer];//使用这个将得到的是JSON

注:
1.后台接收的是json格式,如:{参数:JSON},例:param={userName:13000000000,passWord:123321}
2.默认的Response为json数据,通过添加以上代码,返回的数据将会是一个对象,在success成功的方法块中对responseObject进行处理,id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil]; 转换成json格式。

附完整代码:


- (IBAction)logInClick:(UIButton *)sender {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSMutableDictionary *dict  = [NSMutableDictionary dictionary];
    dict[@"userName"] =  self.phoneTextField.text;
    dict[@"passWord"] = self.passworldTextField.text;
    
    NSString *jsonStr = [NSString stringWithFormat:@"%@", dict];
    
    NSMutableDictionary *params  = [NSMutableDictionary dictionary];
    params[@"param"] =  jsonStr;
    
    [manager POST:[NSString stringWithFormat:@"%@/sjdw/user/appLogin.do",SERVER] parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSLog(@"%@",json);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

未完,持续更新...

你可能感兴趣的:(AFNetworking网络请求常见错误锦集)