我需要你的帮助。我试图解析来自xml的一些数据,并将它们列在listview上。我得到url地址并将其发送到StringReader in AsyncTask。但是,它是null中的reader变量。我也不知道原因。我知道这是个愚蠢的问题,但是,这很令人沮丧,但很重要。有人能解决这个问题吗?这是我的密码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
String st = intent.getStringExtra("State");
String url = "http://whoismyrepresentative.com/getall_reps_bystate.php?state="+st;
/** The parsing of the xml data is done in a non-ui thread */
ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();
/** Start parsing xml data */
listViewLoaderTask.execute(url);
Log.e("URL",url);
}
private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{
/** Doing the parsing of xml data in a non-ui thread */
@Override
protected SimpleAdapter doInBackground(String... url) {
StringReader reader = new StringReader(url[0]);//NULL HERE
System.out.print("READER"+reader);
XmlParser countryXmlParser = new XmlParser();
System.out.print("countryXmlParser"+countryXmlParser);
List<HashMap<String, String>> countries = null;
try{
/** Getting the parsed data as a List construct */
countries = countryXmlParser.parse(reader);
Log.e("@@@","222");
}catch(Exception e){
Log.d("Exception",e.toString());
}
/** Keys used in Hashmap */
String[] from = { "rep","details"};
/** Ids of views in listview_layout */
int[] to = { R.id.tv_country,R.id.tv_country_details};
/** Instantiating an adapter to store each items
* R.layout.listview_layout defines the layout of each item
*/
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.lv_layout, from, to);
return adapter;
}
@Override
protected void onPostExecute(SimpleAdapter adapter) {
/** Getting a reference to listview of main.xml layout file */
ListView listView = ( ListView ) findViewById(R.id.lv_countries);
/** Setting the adapter containing the country list to listview */
listView.setAdapter(adapter); // ERROR THIS LINE这是我的XmlParser课程:
private static final String ns = null;
/** This is the only function need to be called from outside the class */
public List<HashMap<String, String>> parse(Reader reader)
throws XmlPullParserException, IOException{
try{
System.out.print("READER"+reader);
Log.e("###","111");
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
Log.e("###", "222");
parser.setInput(reader);
Log.e("###", "333");
parser.nextTag();
Log.e("###", "444");
return readCountries(parser);
}finally{
}
}
/** This method read each country in the xml data and add it to List */
private List<HashMap<String, String>> readCountries(XmlPullParser parser)
throws XmlPullParserException,IOException{
List<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
parser.require(XmlPullParser.START_TAG, ns, "result");
while(parser.next() != XmlPullParser.END_TAG){
if(parser.getEventType() != XmlPullParser.START_TAG){
continue;
}
String name = parser.getName();
if(name.equals("rep")){
list.add(readCountry(parser));
}
else{
skip(parser);
}
}
return list;
}
/** This method read a country and returns its corresponding HashMap construct */
private HashMap<String, String> readCountry(XmlPullParser parser)
throws XmlPullParserException, IOException{
parser.require(XmlPullParser.START_TAG, ns, "rep");
String countryName = parser.getAttributeValue(ns, "name");
String party = parser.getAttributeValue(ns, "party");
//Log.d("LOG_NAME", parser.getAttributeValue(ns, name));
String details = "Party : " + party;
Log.d("LOG_NAME", details);
Log.d("LOG_NAME1", countryName);
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("rep", countryName);
//hm.put("flag", flag);
hm.put("details",details);
return hm;
}这是我的逻辑猫:
09-18 21:37:53.258 1933-1933/com.example.hoon.myrepresentative E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
at android.widget.ListView.setAdapter(ListView.java:466)
at com.example.hoon.myrepresentative.ListRepActivity$ListViewLoaderTask.onPostExecute(ListRepActivity.java:102)
at com.example.hoon.myrepresentative.ListRepActivity$ListViewLoaderTask.onPostExecute(ListRepActivity.java:57)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)我指的是这个站点
发布于 2015-09-18 14:10:27
我不太确定给StringReader()一个url作为论据是否能起作用。我也做过同样的事情,但是我用HttpURLConnection来阅读网页。我就是这样做的:
url = "URL for xml file online"
URL url_new = new URL(url);
HttpURLConnection connection = (HttpURLConnection) url_new.openConnection();
connection.setRequestMethod("GET");
BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream());
String xml_string = convertInputStreamToString(inputStream);编辑:阅读一些关于StringReader的内容。它接受一个字符串作为参数,并从中生成一个字符流。URL作为字符串提供给它,它可能会生成URL本身的字符流,而不是URL指向的字符流。因此,StringReader无论如何也不会有所帮助。
发布于 2015-09-18 13:54:09
看上去国家都是空的。请参阅SimpleAdapter实现
public int getCount() {
return mData.size();
}mData是你的国家。请粘贴logcat,以便我们可以看到在解析xml时抛出的异常类型。
https://stackoverflow.com/questions/32653699
复制相似问题