Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 2.69 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. HttpURLConection - JSON Response isn't Complete
  2. public void getResponse() throws Exception
  3. {
  4.     if(service.equals("Grooveshark")) link += getHmacMD5(privateGroovesharkKey, jsonInfo.toString());
  5.  
  6.     if(requestedMethod.equals("GET")) infoURL = new URL(link+arguments);
  7.     else infoURL = new URL(link);
  8.  
  9.     HttpURLConnection connection = (HttpURLConnection) infoURL.openConnection();
  10.     connection.setRequestMethod(requestedMethod);
  11.     connection.setRequestProperty("Accept-Charset", "UTF-8");
  12.  
  13. connection.setDoOutput(true);
  14. connection.setDoInput(true);
  15. connection.setUseCaches(false);
  16.  
  17. if(service.equals("Grooveshark"))
  18. {
  19.     connection.setRequestProperty("Content-Type","application/json");
  20.         OutputStream output = connection.getOutputStream();
  21.         output.write(jsonInfo.toString().getBytes());
  22. }
  23. else if(requestedMethod.equals("POST") || requestedMethod.equals("PUT"))
  24. {
  25.     OutputStream output = connection.getOutputStream();
  26.         output.write(arguments.getBytes());
  27. }
  28.  
  29.     connection.connect();
  30.  
  31.     BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  32.     StringBuilder sb = new StringBuilder();
  33.  
  34.     String line;
  35.     while ((line = rd.readLine()) != null)
  36.         sb.append(line).append('n');
  37.  
  38.     setJsonResult(sb.toString());
  39.  
  40.     System.out.println(jsonResult);
  41.  
  42.     jsonFinal = new JSONObject(jsonResult);
  43.  
  44.     connection.disconnect();
  45. }
  46.        
  47. public void performSearch() throws Exception
  48. {
  49.     JSONObject search = new JSONObject();
  50.     search.put("method", method);
  51.  
  52.     JSONObject header = new JSONObject();
  53.     header.put("wsKey", key);
  54.  
  55.     JSONObject parameters = new JSONObject();
  56.     parameters.put("query", getSearchQuery());
  57.     parameters.put("country", "Portugal");
  58.     parameters.put("limit", limit);
  59.     parameters.put("offset", "");
  60.  
  61.     search.put("header", header);
  62.     search.put("parameters", parameters);
  63.  
  64.     JSONHandler jsonHandler = new JSONHandler(link, search, "Grooveshark", "POST", "");
  65.  
  66.     JSONObject finalResult = jsonHandler.getJsonFinal();
  67.  
  68.     JSONArray songs = finalResult.getJSONObject("result").getJSONArray("songs");
  69.  
  70.     ArrayList<Result> allResults = new ArrayList<Result>();
  71.  
  72.     for(int i = 0; i < songs.length(); i++)
  73.     {
  74.         JSONObject inner = (JSONObject) songs.get(i);
  75.         String name = inner.getString("SongName");
  76.         int ID = inner.getInt("SongID");
  77.         String artist = inner.getString("ArtistName");
  78.         Result res = new Result(name, artist, ID);
  79.         res.setAlbumName(inner.getString("AlbumName"));
  80.  
  81.         boolean low = inner.getBoolean("IsLowBitrateAvailable");
  82.         int bit = 0;
  83.  
  84.         if(low) bit = 1;
  85.         else bit = 0;
  86.  
  87.         res.setIsLowBitRateAvailable(bit);
  88.     }
  89.  
  90.     setResults(allResults);
  91. }