代码= -1016"请求失败:不可接受的内容类型:text / plain"的UserInfo = 0x7a2da380

时间:2015-03-11 13:51:26

标签: ios objective-c afnetworking-2 nsresponder

AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"%@",[urlOfVm class]);
    NSMutableString *url = [NSMutableString stringWithString:urlOfVm];

    [url appendString:in];
    NSLog(@"%@",url);

    NSURLRequest* request = [[AFJSONRequestSerializer serializer]requestWithMethod:@"POST" URLString:url parameters:diccp ];
    AFHTTPRequestOperation* operation = [manager HTTPRequestOperationWithRequest:request
                                                                         success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                                                             if (responseObject != nil) {
                                                                                 NSLog(@"----------------------response:%@",[operation responseString]);
                                                                                 NSLog(@"responseObject:%@",responseObject);
                                                                                 [self create:(NSDictionary *)responseObject];

                                                                             }
                                                                             else{
                                                                                 NSLog(@"return null");
                                                                             }



                                                                         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                                             NSLog(@"response:%@",[error description]);
                                                                         }];
    [operation setResponseSerializer:[AFJSONResponseSerializer serializer] ];

    [[NSOperationQueue mainQueue] addOperations:@[operation] waitUntilFinished:NO];

下面是java interFace:

/**
     * user register 
     * if the method return 0 ,it means the user exists; return 1:it means register success, return 2 means the internet has some problem
     * 
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/register")
    @ResponseBody
    public String registerAccount(@RequestBody Account account) {
        System.out.println(account.getUserName());
        Account account1 = accountManager.isExistAccountByUserName(account
                .getUserName());
        if (null != account1) {
            return "0";
        } else {
            try {
                account.setGenerateTime(DateUtils.formatTime(new Date()));
                String id = new SimpleDateFormat("SSSssmmHHddMMyy").format(new Date());
                account.setId(id);
                account.setIdType("1");
                account.setPhoneNumber(account.getUserName());
                accountManager.insertAccount(account);
                return "3";
            } catch (Exception e) {
                return "2";
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

如错误所述,您发送的内容类型错误(text/plain)。我想,您需要将内容类型更改为JSON。为此,请添加JSON请求序列化程序:

[operation setRequestSerializer:[AFJSONRequestSerializer serializer]];