相关疑难解决方法(0)

请求失败:不可接受的内容类型:使用AFNetworking 2.0的text/html

我正在尝试AFNetworking的新版本2.0,我收到了上面的错误.知道为什么会这样吗?这是我的代码:

    NSURL *URL = [NSURL URLWithString:kJSONlink];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    [[NSOperationQueue mainQueue] addOperation:op];
Run Code Online (Sandbox Code Playgroud)

我正在使用Xcode 5.0.

此外,这是错误消息:

Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0xda2e670 {NSErrorFailingURLKey=kJSONlink, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xda35180> { URL: kJSONlink } { status code: 200, headers {
    Connection = "Keep-Alive";
    "Content-Encoding" = gzip;
    "Content-Length" = 2898;
    "Content-Type" = …
Run Code Online (Sandbox Code Playgroud)

ios afnetworking afnetworking-2

201
推荐指数
7
解决办法
17万
查看次数

JSON文本不以数组或对象和选项开头,以允许未设置片段

我正在从服务器发送这个json响应,请求我的IOS 7应用程序.

{
 "root": {
    "success": "1",
    "message": "Successfully retrieved data.",
    "data": {
        "records": [
            {
                "receipt_key": "xxxxxxxx",
                "receipt_id": "xxxxxxxx",
                "store_name": "xxxxxx",
                "amount": "xxxx",
                "date_purchase": "xxxxxxxx",
                "is_processed": "x",
                "created_on": "xxxxxxxx",
                "modified_on": "xxxxxxxx",
                "modified_on_millis": "xxxxxxxx",
                "user_folder": "xxxxxxxx",
                "category_id": "xxxxxxxx",
                "is_deleted": "x",
                "currency_id": "xxxxxxxx"
            }
        ]
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我使用以下代码解析上面的json到NSDictionary对象.

 NSMutableDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
Run Code Online (Sandbox Code Playgroud)

但是我在上面的代码中遇到了这个错误.

Error Domain = NSCocoaErrorDomain Code = 3840"操作无法完成.(Cocoa error 3840.)"(JSON文本不是以数组或对象开头,而是选项允许未设置片段.)UserInfo = 0x8a8a700 {NSDebugDescription = JSON text没有从数组或对象和选项开始,以允许未设置片段.}

iphone ios ios7

12
推荐指数
1
解决办法
4万
查看次数

如何使用AFNetworking发布JSON参数?

我怎么能POST JSON参数AFNetworking?以下是示例JSON格式.先感谢您.

{
    "application": {
        "identifier": "appDev"
    },
    "sso": {
        "version": 2.0,
        "session_expiry": 43200
    },
    "user": {
        "password": "xxxx~!@",
        "username": "xxx0min"
    },
    "coordinate": {
        "latitude": 10.9948629,
        "longitude": -169.5213749
    }
}
Run Code Online (Sandbox Code Playgroud)

我成功创建了一个JSON:

NSDictionary *parameters = @{@"application": @{@"identifier": @"appDev"}, @"sso": @{@"version": @"2.0", @"session_expiry":@43200}, @"user": @{@"password":@"xxxx~!@", @"username":@"xxx0min"}, @"coordinate": @{@"latitude":@10.9948629, @"longitude":@-169.5213749}};
Run Code Online (Sandbox Code Playgroud)

xcode json ios afnetworking afnetworking-2

5
推荐指数
1
解决办法
1万
查看次数

如何在swift中使用NSURLSession来发布JSON数据

我坚持使用下面的代码.如何设置param和post方法?

let params:[String:Any] = [
        "email" : usr,
        "userPwd" : pwdCode]

let url = NSURL(string:"http://inspect.dev.cbre.eu/SyncServices/api/jobmanagement/PlusContactAuthentication")
let request = NSMutableURLRequest(URL: url!)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")

request.HTTPBody = params<what should do for Json parameter>


let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
    data, response, error in

    if let httpResponse = response as? NSHTTPURLResponse {
        if httpResponse.statusCode != 200 {
            println("response was not 200: \(response)")
            return
        }
    }
    if error {
        println("error submitting request: \(error)")
        return
    }

    // handle the data of the successful response here …
Run Code Online (Sandbox Code Playgroud)

post json nsurlsession swift

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

ios ×3

afnetworking ×2

afnetworking-2 ×2

json ×2

ios7 ×1

iphone ×1

nsurlsession ×1

post ×1

swift ×1

xcode ×1