0

I have a problem to get .xml data from website and parse to android application, i have try many suggestion but never work. It works fine when i open file in assets folder, but when i try to get from url, it show nothing

This is my Activity Class

package com.android.baliweather;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;  
import android.widget.TextView;

public class Prakiraan extends Activity {
/** Called when the activity is first created. */
private ListView listView;
private TextView judul;
private String URL_MAIN = "http://localhost/android/propinsi_17_1.xml";

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.prakiraan_cuaca);
    judul = (TextView) findViewById(R.id.menu1);
    listView = (ListView) findViewById(R.id.listview);
    bindDataToListing();
}

private void bindDataToListing() {
    try {
        URL url = new URL("http://localhost/android/propinsi_17_1.xml");
        URLConnection connection = url.openConnection();
        //URLConnection connection = url.openConnection();  
        //HttpURLConnection httpConnection = (HttpURLConnection)connection;
        //InputStream in = httpConnection.getInputStream();
        SAXParserFactory saxparser = SAXParserFactory.newInstance();
        SAXParser parser = saxparser.newSAXParser();
        XMLReader xmlReader = parser.getXMLReader();
        XMLParser pc = new XMLParser();
        xmlReader.setContentHandler(pc);
        //InputStream is = getAssets().open("propinsi_17_1.xml");
        //InputStream is = new URL(URL_MAIN).openStream();
        xmlReader.parse(new InputSource(connection.getInputStream()));
        BindingData bindingData = new BindingData(this, pc.kota, pc.cuaca, pc.suhuMin, pc.suhuMax, pc.kelembapanMin, pc.kelembapanMax, pc.kecepatanAngin, pc.arahAngin);
        listView.setAdapter(bindingData);
    } 
    catch (Exception e) {
        e.getMessage();
    }
}

}

I think the problem only on this class, but i have no idea which one. Please help

  • possible duplicate of [android: Is it possible to dowload file to disk given an xml (from a URL)?](http://stackoverflow.com/questions/5798651/android-is-it-possible-to-dowload-file-to-disk-given-an-xml-from-a-url) – Shabbir Dhangot Aug 21 '14 at 07:28
  • many said that it is possible, check this http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser – arya maharta Aug 21 '14 at 07:42
  • first of all Your directly using local host that will not work. Use `10.0.2.2` instead of localhost then check. for more detail visit here : http://stackoverflow.com/questions/6760585/accessing-localhostport-from-android-emulator – Shabbir Dhangot Aug 21 '14 at 07:48

1 Answers1

0

It might be android.os.NetworkOnMainThreadException - you're trying to do time-heavy task in a UI thread and the system is preventing you from doing it (you can possibly see this in your logcat). You can implement AsyncTask in order to do it in the background thread.

It may also be that you forgot to add the permission in a manifest file - <uses-permission android:name="android.permission.INTERNET" />

cliffroot
  • 1,839
  • 17
  • 27