- HttpURLConection - JSON Response isn't Complete
- public void getResponse() throws Exception
- {
- if(service.equals("Grooveshark")) link += getHmacMD5(privateGroovesharkKey, jsonInfo.toString());
- if(requestedMethod.equals("GET")) infoURL = new URL(link+arguments);
- else infoURL = new URL(link);
- HttpURLConnection connection = (HttpURLConnection) infoURL.openConnection();
- connection.setRequestMethod(requestedMethod);
- connection.setRequestProperty("Accept-Charset", "UTF-8");
- connection.setDoOutput(true);
- connection.setDoInput(true);
- connection.setUseCaches(false);
- if(service.equals("Grooveshark"))
- {
- connection.setRequestProperty("Content-Type","application/json");
- OutputStream output = connection.getOutputStream();
- output.write(jsonInfo.toString().getBytes());
- }
- else if(requestedMethod.equals("POST") || requestedMethod.equals("PUT"))
- {
- OutputStream output = connection.getOutputStream();
- output.write(arguments.getBytes());
- }
- connection.connect();
- BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- StringBuilder sb = new StringBuilder();
- String line;
- while ((line = rd.readLine()) != null)
- sb.append(line).append('n');
- setJsonResult(sb.toString());
- System.out.println(jsonResult);
- jsonFinal = new JSONObject(jsonResult);
- connection.disconnect();
- }
- public void performSearch() throws Exception
- {
- JSONObject search = new JSONObject();
- search.put("method", method);
- JSONObject header = new JSONObject();
- header.put("wsKey", key);
- JSONObject parameters = new JSONObject();
- parameters.put("query", getSearchQuery());
- parameters.put("country", "Portugal");
- parameters.put("limit", limit);
- parameters.put("offset", "");
- search.put("header", header);
- search.put("parameters", parameters);
- JSONHandler jsonHandler = new JSONHandler(link, search, "Grooveshark", "POST", "");
- JSONObject finalResult = jsonHandler.getJsonFinal();
- JSONArray songs = finalResult.getJSONObject("result").getJSONArray("songs");
- ArrayList<Result> allResults = new ArrayList<Result>();
- for(int i = 0; i < songs.length(); i++)
- {
- JSONObject inner = (JSONObject) songs.get(i);
- String name = inner.getString("SongName");
- int ID = inner.getInt("SongID");
- String artist = inner.getString("ArtistName");
- Result res = new Result(name, artist, ID);
- res.setAlbumName(inner.getString("AlbumName"));
- boolean low = inner.getBoolean("IsLowBitrateAvailable");
- int bit = 0;
- if(low) bit = 1;
- else bit = 0;
- res.setIsLowBitRateAvailable(bit);
- }
- setResults(allResults);
- }