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=<NSHTTPURLResponse: 0x7fa135a68900> { 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);
}];
}
未完边臼,持續(xù)更新...