I need your help please. I am trying to parse some data from xml and to list them on listview. I get url address and send it to StringReader
in AsyncTask
. But, it's null
in reader
variable. I don't know why. I know it's really a stupid question but, it's frustrating but important. Can anybody solve this problem? Here's my code:
@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
Here's my XmlParser
class:
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;
}
And here's my logcat:
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)
I refer to this site
I am not really sure whether giving StringReader()
a url as an argument will do the job. I had done exactly the same thing but i used HttpURLConnection
to read a webpage. This is what i did:
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);
EDIT: Read a little about StringReader. It takes a string as a parameter and makes a character stream out of it. The URL was given as a string to it and it probably made a character stream of the URL itself and not of what the URL points to. So StringReader would not have helped anyway.