-2
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接口:

/**
     * 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";
            }
        }
    }
4

1 回答 1

0

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

[operation setRequestSerializer:[AFJSONRequestSerializer serializer]];
于 2015-03-11T14:53:52.927 回答