5

I'm trying to connect to the grooveshark API, this is the http request

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

My question is how can I send this request via Java?

evnu
  • 6,450
  • 2
  • 27
  • 38
Kooshiar Azimian
  • 75
  • 1
  • 2
  • 8

2 Answers2

4

Basically, you can do it with the standard Java API. Check out URL, URLConnection, and maybe HttpURLConnection. They are in package java.net.

As to the API specific signature, try sStringToHMACMD5 found in here.

And remember to CHANGE YOUR API KEY, this is very IMPORTANT, since everyone knows it know.

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();
Community
  • 1
  • 1
xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
  • 1
    Yes, it can and should be a `String` in this case that we are using `PrintWriter`. – xiaofeng.li Nov 19 '12 at 04:51
  • I did that but got the same error "method not defined", I defined String data = "{'method': 'addUserFavoriteSong', 'parameters': {'songID': 0}, 'header': {'wsKey': 'key', 'sessionID': 'sessionID'}}"; – Kooshiar Azimian Nov 19 '12 at 07:30
  • Does the API documentation mention anything specific about the HTTP request? Like what the headers should be, e.g. 'Content-Type'? – xiaofeng.li Nov 19 '12 at 07:44
  • 1
    BTW, standard JSON data should always use double-quote `"` to denote keys and String literals. – xiaofeng.li Nov 19 '12 at 07:45
  • thanks a lot, replaced ' with \" and don't get than error anymore. But get signature error for: String data = "{\"method\":\"getArtistSearchResults\",\"parameters\":{\"query\":\"adele\"},\"header\":{\"wsKey\":\"concertboom\",\"sessionID\":\"0\"}}"; String key = "b07cd9d001e6895956023687e4629f60"; String signature = hash(key, data); – Kooshiar Azimian Nov 19 '12 at 08:50
  • How is your `hash` method implemented? I found a PHP client from grooveshark's developer website, it must be a Java equivalent version of the `hash_hmac` in PHP. – xiaofeng.li Nov 19 '12 at 09:54
0

You could look into the Commons HttpClient package.

It is fairly straight forward to create POST's, specifically you could copy the code found here: 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();
...

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55