0

In order to fetch the content of website and display it in the android app, which task has to be performed first, is that Webservices, Http Response? i had followed many tutorials and links as given in the stackoverflow people, but still could not complete my task, so lastly followed the link Need a simple tutorial for android/webservice work?, getting error in xml.getItemList() in FirstActivity class. Xml file is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android1:id="@+id/listView1"
    android1:layout_width="match_parent"
    android1:layout_height="wrap_content" >
</ListView>

MainActivity code is:

    package com.webservices;

public class FirstActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

          FetchList fl = new FetchList();
           fl.execute();
           }
   //Always better to use async task for these purposes
    public class FetchList extends asyncTask<Void,Void,Byte>{

        protected String doinbackground(string... urls){
             // this was explained in first step
              Response res = new Response("http://www.google.com");
              String response = res.getResponse();
              XMLParser xml = new XMLParser(response);
               ArrayList<item> itemList = xml.getItemList();
               xml.parse();
          };

        public void execute() {
            // TODO Auto-generated method stub
                }
         }
Response class is:
public class Response {
String get_url, response;
Activity activity;

public Response(String url){
    this.get_url = url;

}

public String getResponse(){
     InputStream in = null;        
      byte[] data = new byte[1000];
        try {
              URL url = new URL(get_url);   
              URLConnection conn = url.openConnection();
              conn.connect();
            /*  conn.*/
              in = conn.getInputStream();
              Log.d("Buffer Size +++++++++++++", ""+in.toString().length());
              BufferedReader rd = new BufferedReader(new InputStreamReader(in),in.toString().length());
              String line;
              StringBuilder sb =  new StringBuilder();
              while ((line = rd.readLine()) != null) {
                    sb.append(line);
              }
              rd.close();
              response = sb.toString();

             in.read(data);
          Log.d("INPUT STREAM PROFILE RESPONSE",response);
            in.close();
        } catch (IOException e1) {
            Log.d("CONNECTION  ERROR", "+++++++++++++++++++++++++++");
            // TODO Auto-generated catch block

            e1.printStackTrace();
        }
        return response;
}
Community
  • 1
  • 1
user1825740
  • 29
  • 1
  • 7

1 Answers1

0

Your question is not very clear. Why do you parse the google index page via an xml parser ?

You want to do :

 protected String doinbackground(string... urls){
    HttpUrlConnection conn = new URL( urls[0] ).openConnection();
    BufferedReader reader = new BufferedReader( conn.getInputStream() );
    String s = reader.readFully();    
};

Or something like this (with exception handling).

I advise you not to use an AsyncTask for networking. Have a look at RoboSpice, it is much better suited for network requests.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • i wanted to parse the content of my website and make it displayed in the app, simply for testing i had given google. i had used many tutorials and links which had exception handling, but finally the result is failure. – user1825740 Nov 15 '12 at 08:03
  • Did you try the code I gave you ? Just surround everything with a try catch, search a bit, log any exception and it should work. – Snicolas Nov 15 '12 at 08:09
  • i tried by giving that code in try catch block, cleared the errors, but when i run the project the blank empty screen displays without the website content, what may be the problem? – user1825740 Nov 15 '12 at 12:11
  • return the string s in doInBackground (add an Override annotation to be sure of the spelling). Then in the onPostExecute method of your asyncTask update your ui with the string you receive in parameter – Snicolas Nov 15 '12 at 12:23