Guest User

Untitled

a guest
Jun 8th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3.     private TextView tv;
  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.activity_main);
  8.         Button button = (Button) findViewById(R.id.btnhit);
  9.         tv = (TextView) findViewById(R.id.tv);
  10.         button.setOnClickListener(new View.OnClickListener() {
  11.             @Override
  12.             public void onClick(View v){
  13.                 new JSONtask().execute("http://api.nbp.pl/api/exchangerates/tables/A?format=json");
  14.             }
  15.         });
  16.     }
  17.     public class JSONtask extends AsyncTask<String, String, String>{
  18.  
  19.         @Override
  20.         protected String doInBackground(String... params) {
  21.             HttpURLConnection connection = null;
  22.             BufferedReader reader = null;
  23.             try {
  24.                 URL url = new URL(params[0]);
  25.                 connection = (HttpURLConnection) url.openConnection();
  26.                 connection.connect();
  27.                 InputStream stream = connection.getInputStream();
  28.                 reader = new BufferedReader(new InputStreamReader(stream));
  29.                 StringBuffer buffer = new StringBuffer();
  30.                 String line = "";
  31.                 while ((line = reader.readLine()) != null) {
  32.                     buffer.append(line);
  33.                 }
  34.                 String jsonData = buffer.toString();
  35.                 JSONObject object  = new JSONObject(jsonData);
  36.                 StringBuffer newBuffer = new StringBuffer();
  37.                 JSONArray currencyArray = object.getJSONArray("rates");
  38.                 for(int i = 0; i<currencyArray.length(); i++) {
  39.                     JSONObject currencyObject = currencyArray.getJSONObject(i);
  40.                     String code = currencyObject.getString("code");
  41.                     newBuffer.append(code + "\n");
  42.                 }
  43.  
  44.                 return newBuffer.toString();
  45.             } catch (MalformedURLException e) {
  46.                 e.printStackTrace();
  47.             } catch (IOException e) {
  48.                 e.printStackTrace();
  49.             } catch (JSONException e) {
  50.                 e.printStackTrace();
  51.             } finally {
  52.                 if (connection != null) {
  53.                     connection.disconnect();
  54.                 }
  55.                 try {
  56.                     if (reader != null) {
  57.                         reader.close();
  58.                     }
  59.                 } catch (IOException e) {
  60.                     e.printStackTrace();
  61.                 }
  62.             }
  63.         return null;
  64.         }
  65.  
  66.         @Override
  67.         protected void onPostExecute(String result) {
  68.             super.onPostExecute(result);
  69.             tv.setText(result);
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment