0

My code is here:</p>

AFHTTPRequestOperationManager *managers = [AFHTTPRequestOperationManager manager];

NSMutableDictionary *parDic = [NSMutableDictionary dictionary];

[parDic setObject:@"admin" forKey:@"userId"];

[parDic setObject:@"1" forKey:@"password"];

  [managers POST:@"http://posttestserver.com/post.php?" parameters:par success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
    NSLog(@"sucess");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

It print that:<code>Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/plain"

But when I use kitchen test this request, it works fine.Why?Please help me, thank you very much!

4

2 回答 2

1

按照下面的代码我成功的

NSString *StringUrl = [NSString stringWithFormat:@"%@?userId=%@&password=%@",YourUrl,@"admin",@"1"];

    NSURLRequest *UrlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:StringUrl]];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:UrlRequest];

    operation.responseSerializer = [AFJSONResponseSerializer new];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *OP,id ResponseObj)
     {

         NsLog(@"Success == %@",ResponseObj);


     }failure:^(AFHTTPRequestOperation *OP,NSError *error)
     {

         NsLog(@"Error == %@",[error localizedDescription])

     }];

    [operation start];
于 2013-11-12T05:33:07.597 回答
0

在尝试了其他所有方法后,这条线对我有用:

operation.responseSerializer = [AFCompoundResponseSerializer 序列化器];

NSString *strdata =[NSString stringWithFormat:@"{\"objID\":\"%ld\",\"objString\":\"%@\",\"objWidth\":\"%@\",\"objHeight\":\"%@\",\"objStartX\":\"%@\",\"objStartY\":\"%@\",\"objEndX\":\"%@\",\"objEndY\":\"%@\",\"objSnapX\":\"%@\",\"objSnapY\":\"%@\",\"objImage\":\"%@\"}",
                    (long)self.accelerometerObject.objID,
                    self.accelerometerObject.objString.string,
                    self.accelerometerObject.objWidth,
                    self.accelerometerObject.objHeight,
                    self.accelerometerObject.objStartX,
                    self.accelerometerObject.objStartY,
                    self.accelerometerObject.objEndX,
                    self.accelerometerObject.objEndY,
                    self.accelerometerObject.objSnapX,
                    self.accelerometerObject.objSnapY,
                    self.accelerometerObject.objImage];
NSLog(@"%@",strdata);
NSDictionary *parameters = @{
                             @"data" : strdata,
                             };
AFHTTPRequestOperationManager *requestOperationManager = [AFHTTPRequestOperationManager manager];
requestOperationManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
NSString *requestURL = @"http://yourserver.com/dataHandler.asmx/scriptToHandlePost";
requestOperationManager.responseSerializer = [AFCompoundResponseSerializer serializer];
requestOperationManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[requestOperationManager POST:requestURL
                   parameters:parameters
                      success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     NSLog(@"Succeeded:    >>>>>>> %@",strdata);
 }
                      failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     NSLog(@">>>>>>>>>>>failed %@",error);
 }];
于 2014-08-12T12:41:48.407 回答