iOS AFNetworking 发生故障

Posted

技术标签:

【中文标题】iOS AFNetworking 发生故障【英文标题】:iOS AFNetworking Fail occurring 【发布时间】:2013-02-20 21:55:20 【问题描述】:

错误: MultiValueDictKeyError at /login_or_signup Key 'name' not found in

没有提及任何行号或页面,仅提及来自 Django 设置文件的页面(不是我的代码)

这块 ios 代码失败

    //Now pass this to our login
    NSURL *url = [NSURL URLWithString:BASE_URL];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            name, @"name",
                            email, @"email",
                            fbid, @"fbid",
                            nil];

    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/login_or_signup/" parameters:params];
    NSLog(@"PARAMS %@", params);
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) 
//do something
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) 
        // your failure code here
        NSLog(@"FAILED %@", JSON);
    ]

登录或注册查看代码:

@csrf_exempt
def login_or_signup(request):

    user = False;
    username = False;
    email = "tmp@app.com"
    password = "tmppass"

    displayname = request.POST['name']
    fbid = request.POST['fbid']

    #Do we already have this user?

    if "email" in request.POST:
        email = request.POST['email']
        username = email
        try:
            user = User.objects.get(email=email)
        except User.DoesNotExist:
            pass

    if fbid and not user:
        username = fbid
        try:
            user_profile = UserProfile.objects.get(fbid=fbid)
            user = user_profile.user
        except UserProfile.DoesNotExist:
            pass

    if not user:
        user = User.objects.create_user(fbid, email, password)

    #tastypie api_key
    api = ApiKey.objects.get(user=user)

    #Update user_profile:
    user_profile = user.get_profile()
    user_profile.username = displayname
    user_profile.fbid = fbid
    user_profile.save()

    dic = list()
    dic.append(
        'username':user_profile.username, 
        'user_profile_id':user_profile.id,
        'fbid':fbid,
        'api_username':user.username,
        'api_key':api.key
    )
    return HttpResponse(simplejson.dumps(dic, cls=DjangoJSONEncoder), mimetype='application/json') 

一些方便的 iOS 代码

- (void)request:(FBRequest *)request didLoad:(id)result 

    //Loading details about this FB user.  Send the details to Web App and create and/or login this user.

    NSLog(@"FACEBOOK RESULT %@ ", result);

    NSString * fbid = [result objectForKey:@"id"];
    NSString * email = [result objectForKey:@"email"];
    NSString * name = [result objectForKey:@"name"];

    [self loginOrSignup:name email:email fbid:fbid];

还要注意,上面的这个 NSLog 确实有效,我可以看到 id、email、姓名等。

发送到 loginorsignup iOS 方法,该方法通过 AFNetworking 联系 url

【问题讨论】:

发布错误信息 只渲染了500.html页面 如果您将DEBUG 设置为True,您应该会得到一个信息更丰富的错误页面 谢谢。 MultiValueDictKeyError at /login_or_signup Key 'name' not found in 能否包含 login_or_signup 视图的视图代码? 【参考方案1】:

尝试在您的包含中添加引号:

urlpatterns = patterns('',    
    url(r'^admin/', include(admin.site.urls)),        
    url(r'^upload/', 'image.views.upload_file'),
    url(r'^login_or_signup', 'image.views.login_or_signup'),  
    url(r'^api/', include('v1_api.urls')) // mark the quotes
)

如果您的 urls.py 顶部没有 from django.contrib import admin,也请在 admin.site.urls 上使用引号

【讨论】:

问题已经从一个简单的 url 修复 - 开始处理多值字典键错误是好的 对不起,我在发布之前没有重新加载页面以查看更新 没关系 :) 知道更新后的问题有什么问题 您能从您的 500 页面发布更多信息吗?当我可以看到整个堆栈跟踪时,这会容易得多。它为您提供有关哪些线路失败的线索

以上是关于iOS AFNetworking 发生故障的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发之AFN的基本使用

iOS AFNetworking 3.0以上使用

[AFN]AFNetworking错误总结

AFNetworking 同步调用(like/unlike)

ios开发网络学习AFN三:AFN的序列化

iOS开发AFN使用二:AFN文件下载与文件上传