我正在从网址读取Xml文档并将其添加到列表视图中。但它没有显示任何东西。这是我的logcat。
06-24 10:08:32.550: D/dalvikvm(26678): GC_FOR_ALLOC freed 161K, 13% free 10115K/11568K, paused 22ms, total 22ms
06-24 10:08:32.550: I/dalvikvm-heap(26678): Grow heap (frag case) to 12.209MB for 1127536->byte allocation
06-24 10:08:32.570: D/dalvikvm(26678): GC_FOR_ALLOC freed 2K, 12% free 11213K/12672K, >paused 17ms, total 17ms
06-24 10:08:32.590: D/AbsListView(26678): Get MotionRecognitionManager
06-24 10:08:34.790: D/AbsListView(26678): onVisibilityChanged() is called, visibility : 4
06-24 10:08:34.790: D/AbsListView(26678): unregisterIRListener() is called
06-24 10:08:34.805: D/AbsListView(26678): onVisibilityChanged() is called, visibility : 0
06-24 10:08:34.805: D/AbsListView(26678): unregisterIRListener() is called
06-24 10:08:34.825: D/libEGL(26678): loaded /system/lib/egl/libEGL_mali.so
06-24 10:08:34.830: D/libEGL(26678): loaded /system/lib/egl/libGLESv1_CM_mali.so
06-24 10:08:34.835: D/libEGL(26678): loaded /system/lib/egl/libGLESv2_mali.so
06-24 10:08:34.845: E/(26678): Device driver API match
06-24 10:08:34.845: E/(26678): Device driver API version: 23
06-24 10:08:34.845: E/(26678): User space API version: 23
06-24 10:08:34.845: E/(26678): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 30 09:36:10 KST 2013
06-24 10:08:34.925: D/OpenGLRenderer(26678): Enabling debug mode 0
06-24 10:08:34.925: D/AbsListView(26678): unregisterIRListener() is called
06-24 10:08:34.945: D/AbsListView(26678): unregisterIRListener() is called
06-24 10:08:34.960: D/AbsListView(26678): unregisterIRListener() is called
这是我的代码
public class MainActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
listView = (ListView) findViewById(R.id.listview);
try {
bindDataToListing();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void bindDataToListing()throws ClientProtocolException, IOException {
try{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser mSaxParser = factory.newSAXParser();
XMLReader mXmlReader = mSaxParser.getXMLReader();
ParsingClass pc = new ParsingClass();
mXmlReader.setContentHandler(pc);
InputStream mInputStream = new URL("https://222.165.187.91/ex_rate/XML_LOLC_EXRT.xml").openStream();
mXmlReader.parse(new InputSource(mInputStream));
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();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是BindingData.java类
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 MySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
这是解析类
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);
}
}
这是应用程序清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.isuru.anothertry"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.isuru.anothertry.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
改变这个:
@Override
public Object getItem(int position) {
return null;
}
要:
@Override
public Object getItem(int position) {
return date.get(position);
}
答案 1 :(得分:0)
谢谢大家的支持。对此,我真的非常感激。但我找到了另一种方法。严格模式无法使用它。这是我的解决方案。请使用此方法以避免检查SSL证书。
public void trustEveryone() {
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
public boolean verify(String hostname, SSLSession session) {
return true;
}});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new X509TrustManager(){
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(
context.getSocketFactory());
} catch (Exception e) { // should never happen
e.printStackTrace();
}
}
Then call it before opening the URL.
public void get() {
try
{
trustEveryone();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser mSaxParser = factory.newSAXParser();
XMLReader mXmlReader = mSaxParser.getXMLReader();
mXmlReader.setContentHandler(this);
InputStream mInputStream = new URL(URL_MAIN).openStream();
mXmlReader.parse(new InputSource(mInputStream));
}
catch(Exception e)
{
Log.e(TAG, "Exception: " + e.getMessage());
}
}
这是唯一对我有用的解决方案。我遵循的Sax解析器示例就是这个。
http://www.pcsalt.com/android/xml-parsing-using-saxparser-android/#sthash.Mb1Yw5uH.dpbs