请求正文中的POST JSON字符串(不在URL-Form-Encoded Request中)AFNetworking

时间:2015-05-04 18:31:01

标签: ios objective-c json afnetworking afnetworking-2

我想使用AFNetworking POST请求向服务器发送JSON字符串。 目前我正在尝试使用代码

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSData * data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:data];

[request setTimeoutInterval:120];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.requestSerializer = [AFJSONRequestSerializer serializer];

AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject)

{        NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error)

{

    NSLog(@"Error: %@", error);

}];

[operation start];`

什么都没发生。代码既不会进入成功阻止,也不会进入失败阻止。可能是什么原因?在AFNetworking 2中有没有其他方法可以做到这一点。 注意:请求在POSTMAN上完美运行,并在不到500毫秒内返回响应。

1 个答案:

答案 0 :(得分:0)

这是我的工作代码

public class YourCustomToolbar extends Toolbar {

    public YourCustomToolbar (Context context) {
        super(context);
    }

    public YourCustomToolbar (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public YourCustomToolbar (Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
     }

    @Override
    public void setTitle(CharSequence title) {
        ((TextView) findViewById(R.id.title)).setText(title);
    }

    @Override
    public void setTitle(int title) {
        ((TextView) findViewById(R.id.title)).setText(title);
    }
}

这是配置HTTP正文AFJSONRequestSerializer

的函数
NSDictionary *parameters = [NSDictionary dictionary];//set values here

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];

[manager POST:@"http://example.com/api" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //Do success code
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //do failure code
}];

您正在使用AFJSONRequestSerializer作为请求序列化程序,这意味着,您将从NSDictionary发送JSON字符串作为HTTP正文

#pragma mark - AFURLRequestSerialization

- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
                           withParameters:(id)parameters
                                    error:(NSError *__autoreleasing *)error
{
    NSParameterAssert(request);

    if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
        return [super requestBySerializingRequest:request withParameters:parameters error:error];
    }

    NSMutableURLRequest *mutableRequest = [request mutableCopy];

    [self.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) {
        if (![request valueForHTTPHeaderField:field]) {
            [mutableRequest setValue:value forHTTPHeaderField:field];
        }
    }];

    if (parameters) {
        if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) {
            [mutableRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        }

        [mutableRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error]];
    }

    return mutableRequest;
}

所以你需要做的就是从JSON值中创建NSDictionary,并按上述方式调用。 无需制作JSON字符串。