0

为了获取网站的内容并显示在android应用程序中,首先必须执行哪个任务,是Webservices,Http Response?我已经按照stackoverflow人员中给出的许多教程和链接进行操作,但仍然无法完成我的任务,所以最后按照链接Need a simple tutorial for android/webservice work? ,在 FirstActivity 类的 xml.getItemList() 中出现错误。xml文件是

<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 代码是:

    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;
}
4

1 回答 1

0

你的问题不是很清楚。为什么要通过 xml 解析器解析 google 索引页面?

你想做:

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

或类似的东西(带有异常处理)。

我建议您不要使用 AsyncTask 进行联网。看看RoboSpice,它更适合网络请求。

于 2012-11-15T07:56:56.977 回答