这是我的代码。在给出Internet权限的情况下执行时未显示任何内容。 但它并没有给任何力量关闭错误
public class MainActivity extends Activity {
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
bindDataToListing();
}
private void bindDataToListing() {
try {
SAXParserFactory saxparser = SAXParserFactory.newInstance();
SAXParser parser = saxparser.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
ParsingClass pc = new ParsingClass();
xmlReader.setContentHandler(pc);
URL url = new URL("https://222.165.187.91/ex_rate/XML_LOLC_EXRT.xml");
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)connection;
httpConn.setDoInput(true);
httpConn.setRequestProperty("charset", "utf-8");
int responseCode = httpConn.getResponseCode();
if(responseCode != HttpStatus.SC_OK) {
InputStream xmlStream = httpConn.getInputStream();
xmlReader.parse(new InputSource(xmlStream));
BindingData bindingData = new BindingData(this,pc.date, pc.from_currency,pc.to_currency, pc.exrt_buy,pc.exrt_sell);
listView.setAdapter(bindingData);
}
} catch (Exception e) {
e.getMessage();
}
}
}
public class BindingData extends BaseAdapter {
ArrayList<String> date;
ArrayList<String> from_currency;
ArrayList<String> to_currency;
ArrayList<String> exrt_buy;
ArrayList<String> exrt_sell;
LayoutInflater inflater;
public BindingData() {
}
public BindingData(Activity act,ArrayList<String> date, ArrayList<String> from_currency,
ArrayList<String> to_currency, ArrayList<String> exrt_buy,ArrayList<String> exrt_sell) {
this.date=date;
this.from_currency = from_currency;
this.to_currency = to_currency;
this.exrt_buy = exrt_buy;
this.exrt_sell = exrt_sell;
inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return date.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
holder = new Holder();
convertView = inflater.inflate(R.layout.listview_row, null);
holder.txt_date = (TextView) convertView.findViewById(R.id.date);
holder.txt_FromCurrency = (TextView) convertView.findViewById(R.id.from_currency);
holder. txt_toCurrency = (TextView) convertView.findViewById(R.id.to_currency);
holder.txt_exrtBuy = (TextView) convertView.findViewById(R.id.exrt_buy);
holder.txt_exrtSell = (TextView) convertView.findViewById(R.id.exrt_sell);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.txt_date.setText(Html.fromHtml("" + date.get(position)));
holder.txt_FromCurrency.setText(Html.fromHtml("" + from_currency.get(position)));
holder.txt_toCurrency.setText(Html.fromHtml("<b>To Currency : </b>"+ to_currency.get(position)));
holder.txt_exrtBuy.setText(Html.fromHtml("<b>Buying : </b>" + exrt_buy.get(position)));
holder.txt_exrtSell.setText(Html.fromHtml("<b>Selling : </b>" + exrt_sell.get(position)));
return convertView;
}
private class Holder {
TextView txt_date,txt_FromCurrency, txt_toCurrency, txt_exrtBuy,txt_exrtSell;
}
}
public class ParsingClass extends DefaultHandler {
ArrayList<String> date = new ArrayList<String>();
ArrayList<String> from_currency = new ArrayList<String>();
ArrayList<String> to_currency = new ArrayList<String>();
ArrayList<String> exrt_buy = new ArrayList<String>();
ArrayList<String> exrt_sell = new ArrayList<String>();
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if (localName.equalsIgnoreCase("Date")) {
tempStore = "";
}else if (localName.equalsIgnoreCase("From_Currency")) {
tempStore = "";
} else if (localName.equalsIgnoreCase("To_Currency")) {
tempStore = "";
} else if (localName.equalsIgnoreCase("exrt_buy")) {
tempStore = "";
} else if (localName.equalsIgnoreCase("exrt_sell")) {
tempStore = "";
}else{
tempStore = "";
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if (localName.equalsIgnoreCase("Date")) {
date.add(tempStore);
}
else if (localName.equalsIgnoreCase("From_Currency")) {
from_currency.add(tempStore);
} else if (localName.equalsIgnoreCase("To_Currency")) {
to_currency.add(tempStore);
} else if (localName.equalsIgnoreCase("exrt_buy")) {
exrt_buy.add(tempStore);
} else if (localName.equalsIgnoreCase("exrt_sell")) {
exrt_sell.add(tempStore);
}
tempStore = "";
}
private String tempStore = "";
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
tempStore += new String(ch, start, length);
}
}