0

Now, I have made a code prototype in java which is the following:

URL url = new URL("http://gobettivolta.gov.it/");
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));

String line = null;
String sottostringa = null;

while ((line = br.readLine()) != null) {

    if(line.contains("[Circ")){
        line = line.trim();
        if(line.contains("\t"))
            line = line.substring(0, line.indexOf("\t"));
        if(line.contains("<"))
            line = line.substring(0, line.indexOf("<"));
        System.out.println(line);
    }
}

I think it's pretty straight forward, I just want to get the titles of the articles and print them out in the terminal. Everything works just fine in NetBeans but then I wanted to transfer every title in a list view in android.

I've already prepared the bone structure, so I've got a list view where there's everything I need; I've tried to populate it with examples put in a listArray and everything works there as well.

The thing is putting them two things together, I'd like to place every title in the listArray of the application and show them in the list view.

I've tried to copy the code right away in android studio but nothing shows up other than an example string I placed before the loop.

I've also tried to place another string after the loop and that doesn't show up either. So the cases are two: either the program stops inside the loop or the loop takes forever to end..

The thing is I haven't written anything down in the manifest.. should I do something there?? Or is there something it's not permitted to do in android that I did??

And also, I know I could have used parsing libraries, but considered the easiness of the task I thought it would have been just a waste; so I wrote what I needed by my own..

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listaCircolari = new ArrayList<Circolare>();

    try {
        riempiCircolari();
    } catch(Exception e){}

    ListAdapter adapter = new ListAdapterCircolari(MainActivity.this, listaCircolari);
    ListView list = (ListView) findViewById(R.id.list_view_circolari);
    list.setAdapter(adapter);

}


private void riempiCircolari() throws Exception{

    listaCircolari.add(new Circolare("Pluto"));

    URL url = new URL("http://gobettivolta.gov.it/");
    URLConnection con = url.openConnection();
    InputStream is = con.getInputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line = null;
    String sottostringa = null;

    while ((line = br.readLine()) != null) {

        if(line.contains("[Circ")){
            line = line.trim();
            if(line.contains("\t"))
                line = line.substring(0, line.indexOf("\t"));
            if(line.contains("<"))
                line = line.substring(0, line.indexOf("<"));

            listaCircolari.add(new Circolare(line));
        }

    }
    listaCircolari.add(new Circolare("pippo"));
}

The list adapter works fine and all the rest do I'd avoid putting them here.

Vladimir Markeev
  • 654
  • 10
  • 25

2 Answers2

0

You need to add the internet permission in the Manifest

<uses-permission android:name="android.permission.INTERNET" />
Pztar
  • 4,274
  • 6
  • 33
  • 39
0

You should not make http requests in the UI main thread(e.g in onCreate()). Refer: How to fix android.os.NetworkOnMainThreadException?. Try to put the http request into a Asynctask.

Community
  • 1
  • 1
ChinLoong
  • 1,735
  • 24
  • 26
  • So, I managed to put the while loop in an async task and I've noticed something.. The code does get executed but it's extremely slow something like it analyses a line of the html code every second and worst it stops at the tenth ineration. (I know this because I made a toast show up every iteration showing a counter) I was wandering if there are better ways to parse html code in android because in netbeans it only takes a second, basically to request the html code.. – Edoardo Wijaya Grappolini May 06 '15 at 17:39
  • Probably not a good idea to use ui toast to display intensive logs. Might be better idea to use log instead. http://developer.android.com/reference/android/util/Log.html – ChinLoong May 07 '15 at 13:21
  • uhm.. I'll try to implement logs.. the thing it that I don't think that's the problem.. do you know if there are different methods to parse html?? I don't understand why it takes so long for a parsing though considering that on nowadays android devices you can run intense video games such as dead space for example with no problem at all.. – Edoardo Wijaya Grappolini May 12 '15 at 07:34
  • your code should look different now...probably a good idea to update your code to reflect the latest, in order for others to review. – ChinLoong May 13 '15 at 02:57