|
| 1 | +package edu.hzuapps.androidlabs.homeworks.net1414080903240; |
| 2 | + |
| 3 | +import android.content.SharedPreferences; |
| 4 | +import android.content.pm.ProviderInfo; |
| 5 | +import android.graphics.Bitmap; |
| 6 | +import android.graphics.BitmapFactory; |
| 7 | +import android.os.Message; |
| 8 | +import android.support.v7.app.AppCompatActivity; |
| 9 | +import android.os.Bundle; |
| 10 | + |
| 11 | +import android.os.Handler; |
| 12 | +import android.util.Xml; |
| 13 | +import android.widget.ListView; |
| 14 | +import android.widget.SimpleAdapter; |
| 15 | + |
| 16 | +import org.xmlpull.v1.XmlPullParser; |
| 17 | + |
| 18 | +import java.io.InputStream; |
| 19 | +import java.net.HttpURLConnection; |
| 20 | +import java.net.URL; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import java.util.logging.LogRecord; |
| 26 | + |
| 27 | +public class Net1414080903240_OldAccount extends AppCompatActivity { |
| 28 | + private ListView mListView; |
| 29 | + private String path="https://raw.githubusercontent.com/mokulai/android-labs-2017/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903240/detailvalues.xml"; |
| 30 | + Handler mHandler=new Handler(){ |
| 31 | + public void handleMessage(Message msg) { |
| 32 | + if(msg.what==1){ |
| 33 | + try { |
| 34 | + List<order> info=(List<order>) msg.obj; |
| 35 | + initData(info); |
| 36 | + }catch (Exception e) { |
| 37 | + e.printStackTrace(); |
| 38 | + } |
| 39 | + }else{ |
| 40 | + try { |
| 41 | + List<order> info=getAccountInfos(getResources().getAssets().open("detailvalues.xml")); |
| 42 | + initData(info); |
| 43 | + }catch (Exception e) { |
| 44 | + e.printStackTrace(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + } |
| 49 | + }; |
| 50 | + protected void onCreate(Bundle savedInstanceState) { |
| 51 | + super.onCreate(savedInstanceState); |
| 52 | + setContentView(R.layout.old_account); |
| 53 | + getXml(); |
| 54 | + } |
| 55 | + protected void initData(List<order> info) { |
| 56 | + ListView lv = (ListView) findViewById(R.id.lv); |
| 57 | + try { |
| 58 | + ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String,Object>>(); |
| 59 | + for(order infos:info) |
| 60 | + { |
| 61 | + HashMap<String, Object> map = new HashMap<String, Object>(); |
| 62 | + map.put("tt_value",infos.getClassname()); |
| 63 | + map.put("project_value",infos.getValue()); |
| 64 | + listItem.add(map); |
| 65 | + } |
| 66 | + SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,R.layout.list_item, new String[] {"tt_value", "project_value"},new int[]{R.id.tt_value,R.id.project_value}); |
| 67 | + lv.setAdapter(mSimpleAdapter); |
| 68 | + |
| 69 | + } catch (Exception e) { |
| 70 | + e.printStackTrace(); |
| 71 | + } |
| 72 | + } |
| 73 | + public static List<order> getAccountInfos( InputStream is) throws Exception { |
| 74 | + XmlPullParser parser = Xml.newPullParser(); |
| 75 | + parser.setInput(is, "utf-8"); |
| 76 | + List<order> infos = null; |
| 77 | + order info = null; |
| 78 | + int type = parser.getEventType(); |
| 79 | + while (type != XmlPullParser.END_DOCUMENT) { |
| 80 | + switch (type) { |
| 81 | + case XmlPullParser.START_TAG: |
| 82 | + if ("detail".equals(parser.getName())) { |
| 83 | + infos = new ArrayList<order>(); |
| 84 | + } else if ("account".equals(parser.getName())) { |
| 85 | + info = new order(); |
| 86 | + String id = parser.getAttributeValue(0); |
| 87 | + info.setId(Integer.parseInt(id)); |
| 88 | + } else if ("classname".equals(parser.getName())) { |
| 89 | + String cla = parser.nextText(); |
| 90 | + info.setClassname(cla); |
| 91 | + } else if ("value".equals(parser.getName())) { |
| 92 | + String value = parser.nextText(); |
| 93 | + info.setValue(value); |
| 94 | + } |
| 95 | + break; |
| 96 | + case XmlPullParser.END_TAG: |
| 97 | + if ("account".equals(parser.getName())) { |
| 98 | + infos.add(info); |
| 99 | + info = null; |
| 100 | + } |
| 101 | + break; |
| 102 | + |
| 103 | + } |
| 104 | + type = parser.next(); |
| 105 | + } |
| 106 | + return infos; |
| 107 | + } |
| 108 | + |
| 109 | + public void getXml(){ |
| 110 | + new Thread(new Runnable() { |
| 111 | + private HttpURLConnection conn; |
| 112 | + public void run() { |
| 113 | + try { |
| 114 | + URL url=new URL(path); |
| 115 | + conn = (HttpURLConnection) url.openConnection(); |
| 116 | + conn.setReadTimeout(5000); |
| 117 | + conn.setRequestMethod("GET"); |
| 118 | + if (conn.getResponseCode()==200) { |
| 119 | + InputStream inputStream=conn.getInputStream(); |
| 120 | + List<order> listNews=getAccountInfos(inputStream); |
| 121 | + if(listNews.size()>0) { |
| 122 | + Message msg = new Message(); |
| 123 | + msg.obj = listNews; |
| 124 | + msg.what = 1; |
| 125 | + mHandler.sendMessage(msg); |
| 126 | + } |
| 127 | + } |
| 128 | + } catch (Exception e) { |
| 129 | + e.printStackTrace(); |
| 130 | + } |
| 131 | + } |
| 132 | + }).start(); |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments