1

私は周りを検索しようとしましたが、正確な答えを見つけることができません。

私の友人は、以下のように cURL を使用して JSON でデータを返す Web サイトを実装しています。

curl http://example.com//api/v1/opens_set_current_position -d '{"latitude":"53.041", "longitude":"-2.90545", "radius":"100000"}' -X GET -H "Content-type: application/json"

iOS アプリでデータを取得するコードを作成しようとしました。しかし、私はできません。NSURLConnection経由でデータを読み込めないというエラーが常に発生します。これが私のコードです:

NSString *requestString = [NSString stringWithFormat:
                         @"http://example.com//api/v1/opens_set_current_position"];

NSURL *url = [NSURL URLWithString:requestString];

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"GET"];

[req addValue:@"application/json" forHTTPHeaderField:@"Content-type"];

NSString *dataString = @"{\"latitude\":\"53.041\", \"longitude\":\"-2.90545\", \"radius\":\"100000\"}";

NSData *requestData = [NSData dataWithBytes:[dataString UTF8String] length:[dataString length]];

[req setHTTPBody:requestData];

NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&theResponse error:&theError];

データはまったく来ませんでした。次のエラー メッセージが表示されます。

操作を完了できませんでした。(kCFErrorDomainCFNetwork エラー 303。)

誰か助けてくれませんか?

4

1 に答える 1

3

これは、GETリクエストで本文を渡すためです。POST代わりにリクエストが必要です。

マンページからcurl

-d/--data <data>
(HTTP)  Sends  the  specified data in a POST request to the HTTP server,
in the same way that a browser does when a user has filled in an HTML form
and presses the submit button. This will cause curl to pass the data to the
server using the content-type application/x-www-form-urlencoded.
于 2013-03-15T16:02:05.353 に答える