Kloudy

readJSONFromUrl

Jul 10th, 2016
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. public JSONObject readJSONFromURL(String url)
  2. {
  3. JSONObject json = null;
  4. InputStream inputStream = null;
  5. try
  6. {
  7. inputStream = new URL(url).openStream();
  8. InputStreamReader inputReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
  9. BufferedReader bufferedReader = new BufferedReader(inputReader);
  10.  
  11. StringBuilder strBuilder = new StringBuilder();
  12. int character;
  13.  
  14. while ((character = bufferedReader.read()) != -1)
  15. {
  16. strBuilder.append((char) character);
  17. }
  18.  
  19. String jsonText = strBuilder.toString();
  20. json = new JSONObject(jsonText);
  21. }
  22. catch(Exception e){}
  23. finally
  24. {
  25. try
  26. {
  27. inputStream.close();
  28. }
  29. catch (IOException e) {}
  30. }
  31.  
  32. return json;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment