0

次の形式でjsonを作成し、サーバーに投稿しようとしています。

{
"items":  [
 {
  "id": "3",
  "quantity": 0.5,
  "unit": "2"
 } ]
}

を使用してデータを投稿してASIFormDataRequestいます。リクエストパラメータとしてURLで送信するためにjson文字列をエスケープしました

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.esha.com/analysis?items=%@&apikey=%@",encodeJson,ApiKey]];
//here i have send escaped json (do I really need to do this thing?)

_request = [ASIFormDataRequest requestWithURL:url];
[_request setPostValue:jsonString forKey:@"items"]; //original json string
[_request setPostValue:ApiKey forKey:@"apikey"];
[_request setRequestMethod:@"POST"];
[_request addRequestHeader:@"Content-Type" value:@"application/json"];
[_request setDelegate:self];
_request.timeOutSeconds = 60.0; 
[_request startSynchronous];

応答として、HTML形式で「サポートされていないメディアタイプ」エラーが発生します。

<body>
<h1>HTTP Status 415 - Unsupported Media Type</h1>
<HR size="1" noshade="noshade"><p><b>type</b>
Status report</p><p><b>message</b>
<u>Unsupported Media Type</u></p>
<p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Unsupported Media Type).</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.23</h3>
</body>

私はどのような間違いをすることができますか?誰かが私を正しい道に導くことができますか?ありがとう。

4

1 に答える 1

0

最後に私は自分自身を解決します。私の間違いは、サーバーがそのような要求をサポートする方法を持っていない可能性があるのに、要求のためにURLにjson(エンコードされたものまたは何)を投稿しようとしていたことでした。また、ASIHTTPRequestを試してみましたが、ASIFormRequestが機能しなかった理由がわかりません。今、私は応答を得ています。

だから私は上記のコードを次のように変更しました:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.esha.com/analysis?apikey=%@",ApiKey]];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request setPostBody:(NSMutableData *)[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[request setDelegate:self];
[request startSynchronous];
于 2012-11-10T16:26:14.397 に答える