Guest User

Untitled

a guest
Mar 13th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. public void Metoda(View v) throws IOException, JSONException
  2.     {
  3.         TextView tv = (TextView) findViewById(R.id.wyswietl);
  4.  
  5.         JSONObject jsonObject = readJSONFromURL("http://api.fixer.io/latest?base=USD");
  6.         String s =  jsonObject.get("rates").toString();
  7.  
  8.  
  9.         tv.setText(s);
  10.     }
  11.  
  12.     static JSONObject readJSONFromURL(String url) throws IOException, JSONException
  13.     {
  14.  
  15.         InputStream inputStream =  new URL(url).openStream();
  16.  
  17.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
  18.  
  19.         String jsonText = readAllFromJSON(bufferedReader);
  20.  
  21.         return new JSONObject(jsonText);
  22.     }
  23.  
  24.     private static String readAllFromJSON(BufferedReader bufferedReader) throws IOException
  25.     {
  26.         StringBuilder stringBuilder = new StringBuilder();
  27.         int i;
  28.         while((i = bufferedReader.read()) != -1)
  29.         {
  30.             stringBuilder.append((char) i);
  31.         }
  32.  
  33.         return stringBuilder.toString();
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment