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?

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, it can and should be a String in this case that we are using PrintWriter.
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'}}";
Does the API documentation mention anything specific about the HTTP request? Like what the headers should be, e.g. 'Content-Type'?
BTW, standard JSON data should always use double-quote " to denote keys and String literals.
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);
|
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,

2 Comments

PostMethod doesn't exist in HttpClient 4.2.2
You will note that the link is for the 3.x version. I'm quite sure something similar exists in 4.2.2 and anyway, you are expected to provide some work yourself, given the guidance from others here :-) Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.