2013-08-30 224 views

回答

1

您確定您正在POST'ing?上述網址看起來像GET。

<html><head> 
<meta http-equiv="content-type" content="text/html;charset=utf-8"> 
<title>405 HTTP method GET is not supported by this URL</title> 
</head> 
<body text=#000000 bgcolor=#ffffff> 
<h1>Error: HTTP method GET is not supported by this URL</h1> 
</body></html> 
1

作爲每API documentation你應該傳遞JSON數據作爲POST方法的請求主體,而不是作爲URL參數。

因此,它應該是這個樣子:

String data = "{\"data\": [{\"text\": \"I love Titanic.\"}, {\"text\": \"I hate Titanic.\"}]}"; 

URL url = new URL("http://www.sentiment140.com/api/[email protected]"); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
connection.setDoInput(true); 
connection.setDoOutput(true); 
connection.setRequestMethod("POST"); 

// write the request body 
connection.getOutputStream().write(data.getBytes("UTF8")); 

// get the response and read it 
InputStream in = connection.getInputStream();