下面给出的代码中是否有任何错误?

时间:2011-11-11 07:51:50

标签: android android-asynctask

我遇到运行以下代码的问题。代码没有错误但没有给出预期的输出。任何建议或解决方案都将受到高度赞赏。提前提前。

这是我的xml文件: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/readWebpage"
            android:text="Load Webpage"
            android:onClick="readWebpage"/>

    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/textView1"
              android:text="Example Text"
              android:layout_gravity="center_horizontal"/>

</LinearLayout>

这是我的java文件: -

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class LoadWebpageAsyncTask extends Activity {

    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        tv = (TextView)findViewById(R.id.textView1);

    }
    public class DownloadWebpageTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... urls) {
            // TODO Auto-generated method stub

            String response = "";
            for(String url:urls){
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try{
                    HttpResponse execute = client.execute(httpGet);
                    BufferedReader br = new BufferedReader(new 
                            InputStreamReader(execute.getEntity().getContent()));
                    String s="";
                    while((s = br.readLine()) != null){

                        response += s;
                    }
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
            return response;
        }


        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            tv.setText(result);
        }

    }

        public void readWebpage(View view){ 
            DownloadWebpageTask task = new DownloadWebpageTask();
            task.execute(new String[]{"http://www.google.com"});

            }
}

2 个答案:

答案 0 :(得分:2)

您的setContentView(R.layout.<...>);方法中似乎缺少tvonCreate()将为空)并且永远不会执行您的任务(因此您不会获得NullPointerExceptiontv)上设置文字:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv = (TextView)findViewById(R.id.textView1);
    readWebpage(null); //you never use argument anyway
}

编辑 riha的答案也是如此,因为您使用它的方式是for (String[] url:urls),而不是for (String url:urls)

答案 1 :(得分:1)

我认为你不需要为execute()提供一个String数组。这应该足够了:

task.execute("http://www.google.com");

您可以提供以下几个网址:

task.execute("url1", "url2");

如果仍然无法正常工作,请进行一些调试:获取AsyncTask的代码并将其放在onCreate()中。在它上面放一个断点,仔细看看它有什么问题。