I want to take currency from specific Url but AsyncTask is giving me runtime error.I couldnt find what I have to do.This is my first all about parsing.So Thanks a lot :)
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebServisXML xmlRead = new WebServisXML(this); xmlRead.execute("http://www.tcmb.gov.tr/kurlar/today.xml"); } public class WebServisXML extends AsyncTask<String, String, List<String>> { private Context context; private ListView listView; private ProgressDialog dialogBar; List<String> dovizListe = new ArrayList<String>(); HttpURLConnection baglanti = null; public WebServisXML(Context context) { this.context = context; listView = (ListView) ((AppCompatActivity) context).findViewById(R.id.listView);} protected void onPreExecute() { super.onPreExecute(); dialogBar = ProgressDialog.show(context, "Lütfen bekleyiniz...", "İşlem yükleniyor...", true); } @Override protected List<String> doInBackground(String... params) { try { URL adressOfLink = new URL(params[0]); baglanti = (HttpURLConnection) adressOfLink.openConnection(); int responceCode = baglanti.getResponseCode(); if (responceCode == HttpURLConnection.HTTP_OK) { BufferedInputStream stream = new BufferedInputStream(baglanti.getInputStream()); publishProgress("Döviz kurları okunuyor..."); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(stream); NodeList dovizNodeList = document.getElementsByTagName("Currency"); for (int i = 0; i < dovizNodeList.getLength(); i++) { Element element = (Element) dovizNodeList.item(i); NodeList nodeBirim = element.getElementsByTagName("Unit"); NodeList nodeIsim = element.getElementsByTagName("Isim"); NodeList nodeAlis = element.getElementsByTagName("ForexBuying"); NodeList nodeSatis = element.getElementsByTagName("ForexSelling"); String birim = nodeBirim.item(0).getFirstChild().getNodeValue(); String isim = nodeIsim.item(0).getFirstChild().getNodeValue(); String alis = nodeAlis.item(0).getFirstChild().getNodeValue(); String satis = nodeSatis.item(0).getFirstChild().getNodeValue(); dovizListe.add(birim + " " + isim + " Alış:" + alis + " Satış:" + satis); } publishProgress("Liste güncelleniyor..."); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); }finally { if(baglanti!=null) baglanti.disconnect(); } return dovizListe; } @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); dialogBar.setMessage(values[0]); } @Override protected void onPostExecute(List<String> strings) { super.onPostExecute(strings); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context,android.R.layout.simple_expandable_list_item_1,strings); listView.setAdapter(arrayAdapter); dialogBar.cancel(); } }}
This is my error screen: enter image description here
No comments:
Post a Comment