5

グルーブシャーク API に接続しようとしています。これは http リクエストです

POST URL
http://api.grooveshark.com/ws3.php?sig=f699614eba23b4b528cb830305a9fc77
POST payload
{"method":'addUserFavoriteSong",'parameters":{"songID":30547543},"header": 
{"wsKey":'key","sessionID":'df8fec35811a6b240808563d9f72fa2'}}

私の質問は、このリクエストをJava経由で送信するにはどうすればよいですか?

4

2 に答える 2

4

基本的には、標準のJavaAPIを使用して実行できます。URL、、URLConnectionそして多分をチェックしてくださいHttpURLConnection。それらはパッケージに入っていますjava.net

API固有の署名についてsStringToHMACMD5は、こちらをご覧ください。

また、APIキーを変更することを忘れないでください。誰もが知っているので、これは非常に重要です。

String payload = "{\"method\": \"addUserFavoriteSong\", ....}";
String key = ""; // Your api key.
String sig = sStringToHMACMD5(payload, key);

URL url = new URL("http://api.grooveshark.com/ws3.php?sig=" + sig);
URLConnection connection = url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);

connection.connect();

OutputStream os = connection.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
pw.write(payload);
pw.close();

InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
    sb.append(line);
}
is.close();
String response = sb.toString();
于 2012-11-15T11:11:01.117 に答える
0

CommonsHttpClientパッケージを調べることができます。

を作成するのはかなり簡単ですPOST。具体的には、次のコードをコピーできます: http: //hc.apache.org/httpclient-3.x/methods/post.html

PostMethod post = new PostMethod( "http://api.grooveshark.com/ws3.php?sig=f699614eba23b4b528cb830305a9fc77" );
NameValuePair[] data = {
    new NameValuePair( "method", "addUserFavoriteSong..." ),
    ...
};
post.setRequestBody(data);
InputStream in = post.getResponseBodyAsStream();
...

乾杯、

于 2012-11-15T11:11:08.170 に答える