Help fetching data from the web

Put your problem here if it does not fit any of the other categories.

Help fetching data from the web

Postby brakim » Tue Feb 08, 2011 9:13 pm

Hi,

I just want to say that i'am supernew to both android and java, i have erlier coded in php so i know the basic logic in codeing.

I'am just for fun now trying to make a app that is fetching data from a website, actually a local newpaper where i live.

I basically have two problem to begin with, the first one is that it only loads data to fill the screen and nothing more, and how i proceed to chose what data to pick, i mean if a can somehow choose to only get all the <h3> tags in a list.

Here is my code
Code: Select all
package com.kimbrannlund.lesson2;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Lesson2Main extends Activity {
        @Override
        public void onCreate(Bundle icicle) {
                super.onCreate(icicle);

                /* We will show the data we read in a TextView. */
                TextView tv = new TextView(this);
               
                /* Will be filled and displayed later. */
                String norranData = null;
                try {
                        /* Define the URL we want to load data from. */
                        URL norranURL = new URL(
                              "http://norran.se/nyheter/norrochvasterbotten/");
                       
                        /* Open a connection to that URL. */
                        URLConnection ucon = norranURL.openConnection();

                        /* Define InputStreams to read from the URLConnection. */
                        InputStream is = ucon.getInputStream();
                        BufferedInputStream bis = new BufferedInputStream(is);
                       
                        /* Read bytes to the Buffer until there is nothing more to read(-1). */
                        ByteArrayBuffer baf = new ByteArrayBuffer(50000);
                        int current = 0;
                        while((current = bis.read()) != -1){
                                baf.append((byte)current);
                        }

                        /* Convert the Bytes read to a String. */
                        norranData = new String(baf.toByteArray());
                } catch (Exception e) {
                        /* On any Error we want to display it. */
                      norranData = e.getMessage();
                }
               
                /* Show the String on the GUI. */
                tv.setText(norranData);
                this.setContentView(tv);
        }
}


Would be super thansful also if someone knew a good tutorial for this kind of app deveolopment.
brakim
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Feb 08, 2011 8:39 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests