Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. private class GetRbcExchangeRates extends AsyncTask<String, String, String> {
  2.  
  3. public ArrayList<CurrencyRateModel> listOfCurrency = new ArrayList<>();
  4. private final String httpAddress = "https://www.cbr-xml-daily.ru/daily_json.js";
  5.  
  6. @Override
  7. protected String doInBackground(String... params) {
  8.  
  9. HttpURLConnection connection = null;
  10. BufferedReader bufferedReader = null;
  11. StringBuilder jSonResult = new StringBuilder();
  12.  
  13. //listOfCurrency = new ArrayList<>();
  14.  
  15. try {
  16. URL url = new URL(httpAddress);
  17. connection = (HttpURLConnection)url.openConnection();
  18. connection.connect();
  19.  
  20. bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  21.  
  22. String line = "";
  23.  
  24. while((line = bufferedReader.readLine()) != null) {
  25.  
  26. jSonResult.append(line);
  27.  
  28. }
  29.  
  30.  
  31. }
  32. catch(Exception e) {
  33. e.printStackTrace();
  34. publishProgress("MESSAGE", "Отсутствует подключение к интернет!");
  35. }
  36. finally {
  37.  
  38. if(connection != null)
  39. connection.disconnect();
  40.  
  41. try {
  42. if(bufferedReader !=null)
  43. bufferedReader.close();
  44. }
  45. catch(IOException e) {
  46. e.printStackTrace();
  47. }
  48.  
  49. }
  50.  
  51. try {
  52.  
  53. JSONObject rbcGETRates = new JSONObject(jSonResult.toString());
  54. JSONObject jSonValute = rbcGETRates.getJSONObject("Valute");
  55. Iterator<String> arrayKey = jSonValute.keys();
  56.  
  57. while(arrayKey.hasNext()) {
  58.  
  59. String key = arrayKey.next();
  60. JSONObject jSonItem = jSonValute.getJSONObject(key);
  61. CurrencyRateModel currencyitems = new CurrencyRateModel();
  62. currencyitems.id = jSonItem.getString("ID");
  63. currencyitems.nameCode = jSonItem.getString("NumCode");
  64. currencyitems.charCode = jSonItem.getString("CharCode");
  65. currencyitems.nominal = jSonItem.getInt("Nominal");
  66. currencyitems.name = jSonItem.getString("Name");
  67. currencyitems.value = jSonItem.getDouble("Value");
  68. currencyitems.previous = jSonItem.getDouble("Previous");
  69. listOfCurrency.add(currencyitems);
  70. }
  71. }
  72. catch (JSONException e) {
  73.  
  74. e.printStackTrace();
  75. }
  76.  
  77. return null;
  78. }
  79.  
  80. @Override
  81. protected void onPreExecute() {
  82. super.onPreExecute();
  83.  
  84. ListView lsView = (ListView)findViewById(R.id.currencyView);
  85. MyAdapter myAdapter = new MyAdapter(MainActivity.this, listOfCurrency);
  86. lsView.setAdapter(myAdapter);
  87. }
  88.  
  89. @Override
  90. protected void onProgressUpdate(String... values) {
  91. super.onProgressUpdate(values);
  92. if(values[0].equals("MESSAGE")) {
  93. Toast.makeText(MainActivity.this, values[1], Toast.LENGTH_LONG).show();
  94. }
  95. }
  96.  
  97. @Override
  98. protected void onPostExecute(String s) {
  99. super.onPostExecute(s);
  100. }
  101. }
  102.  
  103. public class CurrencyRateModel {
  104.  
  105. public String id;
  106. public String nameCode;
  107. public String charCode;
  108. public int nominal;
  109. public String name;
  110. double value;
  111. double previous;
  112.  
  113. public class MyAdapter extends BaseAdapter {
  114.  
  115. private Context ctx;
  116. private ArrayList<CurrencyRateModel> arrayList;
  117. private LayoutInflater inflater;
  118.  
  119. public MyAdapter(Context ctx, ArrayList<CurrencyRateModel> arrayList) {
  120.  
  121. this.ctx = ctx;
  122. this.arrayList = arrayList;
  123. inflater = ((Activity)ctx).getLayoutInflater();
  124. }
  125.  
  126. @Override
  127. public int getCount() {
  128. return arrayList.size();
  129. }
  130.  
  131. @Override
  132. public Object getItem(int position) {
  133. return null;
  134. }
  135.  
  136. @Override
  137. public long getItemId(int position) {
  138. return position;
  139. }
  140.  
  141. @Override
  142. public View getView(int position, View convertView, ViewGroup parent) {
  143.  
  144.  
  145. ViewHolder vh;
  146.  
  147. if(convertView == null) {
  148.  
  149. vh = new ViewHolder();
  150. convertView = inflater.inflate(R.layout.itemcurrency, null);
  151. vh.tvTitle = (TextView)convertView.findViewById(R.id.title);
  152. vh.tvDescription = (TextView)convertView.findViewById(R.id.description);
  153. convertView.setTag(vh);
  154.  
  155. }
  156. else {
  157.  
  158. vh = (ViewHolder) convertView.getTag();
  159. vh.tvTitle.setText("1");
  160. vh.tvDescription.setText("2");
  161. }
  162.  
  163. return convertView;
  164. }
  165.  
  166. static class ViewHolder {
  167.  
  168. TextView tvTitle, tvDescription;
  169. }
  170.  
  171. @Override
  172. protected void onCreate(Bundle savedInstanceState) {
  173. super.onCreate(savedInstanceState);
  174. setContentView(R.layout.activity_main);
  175.  
  176. // async TASK Run
  177. GetRbcExchangeRates async = new GetRbcExchangeRates();
  178. async.execute();
  179. }
  180.  
  181. <TextView
  182. android:id="@+id/title"
  183. android:layout_width="match_parent"
  184. android:layout_height="wrap_content"
  185. android:textSize="22sp"/>
  186. <TextView
  187. android:id="@+id/description"
  188. android:layout_width="match_parent"
  189. android:layout_height="wrap_content"
  190. android:textSize="18sp"
  191. android:textColor="#555"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement