1

PUT メソッドを使用して NSUrlConnection を介してデータを更新しようとしています。データを追加するために POST メソッドを使用していますが、正常に動作していますが、同時に PUT は動作していません。

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@update/%@?userId=%@",xapp.urlString,[dict valueForKey:@"id"],[profileDict valueForKey:@"id"]] ];
        NSError *error;
        NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"PUT"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:jsonData];
    updateHiveConnection=[NSURLConnection connectionWithRequest:request delegate:self];

JSON データをこの API {"id":3,"active":false} に送信した後、エラーが発生しました

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No value.) UserInfo=0x1fb456f0 {NSDebugDescription=No value.}

どんな助けでも感謝します

4

2 に答える 2

4

私は同じ問題を抱えていましたが、いくつかのGoogle検索の後、次のコード行をに追加するとそれが実行されることがわかりましたNSMutableURLRequest *req:

[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

Jeff LaMarche ブログでこれを見つけました: http://iphonedevelopment.blogspot.ro/2008/06/http-put-and-nsmutableurlrequest.html

于 2014-03-25T12:50:56.940 に答える
1

つまり、実際には JSON を持っていないように見えます。

これには値が含まれていますか?

xapp.urlString,[dict valueForKey:@"id"],[profileDict valueForKey:@"id"]]

オプション NSJSONReadingAllowFragments を [NSJSONSerialization JSONObjectWithData:options:error:] に渡さない限り、サーバーからの応答は、配列またはディクショナリである最上位のコンテナーを持つ有効な JSON である必要があります。

例えば:

{ "response" : "Success" }

PS可変辞書が必要な場合は、オプションに NSJSONReadingMutableContainers も含める必要があります。

于 2013-10-25T07:52:16.177 に答える