2012-11-10 49 views
0

JSON我試圖產生JSON在下面的表格,並上傳到服務器:不支持的媒體類型錯誤 - 從發佈iPhone應用程序

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

我使用ASIFormDataRequest發佈數據。

<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> 

可我是什麼錯誤:我曾經爲了url中把它作爲請求參數

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格式「不支持的媒體類型」錯誤轉義的JSON字符串在做什麼?任何人都可以引導我走向正確的道路?謝謝。

回答

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]; 
相關問題