Android,使用异步任务从网址抓取文本

时间:2013-04-05 13:34:34

标签: java android url android-asynctask

对,我认为,就这一点而言,你应该认为我是一个完整的菜鸟,我在我的头上。

我试图通过几个部分教程来拼凑我对此的理解,每个教程都涉及我想要做的不同方面。

它的长短,我想要做的就是从URL中读取文本以在文本视图中显示,我的理解是,自3.0更新以来我需要一个AsyncTask来做到这一点。

对于我出错的地方的任何帮助都会很棒,因为我不明白导致错误的原因以及如何修复。

我知道doInBackground的try和catch中包含的代码在3.0更新之前就可以自行运行。是否需要修改它以使用Async,但我不知道。

我还想象,一旦这个问题被排序,可能会出现其他问题,如果有人发现明显的东西,如果你能指出它,我将不胜感激。

String Event;
TextView eventText;
TextView titleText;

String HTML;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newevent);

    titleText = (TextView) findViewById(R.id.Title);
    eventText = (TextView) findViewById(R.id.Event);

    new eventupdate().execute();


}

public class eventupdate extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... url) {

        try {

            Thread.sleep(4000);                 

            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpGet httpGet = new HttpGet(
                    "http://masterzangetsu.eu/Apps/rocksoctest"); // URL!
            HttpResponse response = httpClient.execute(httpGet,
                    localContext);
            String result = "";

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));

            String line = null;
            while ((line = reader.readLine()) != null) {
                result += line + "\n";
                HTML = result;
            }

        } catch (InterruptedException e) {

            e.printStackTrace();


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        eventText.setText("" + HTML);

        // TODO Auto-generated method stub
        return null;
    }


}

LogCat错误是:

W/dalvikvm(394): threadid=9: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime(394): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(394): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(394):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(394):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
E/AndroidRuntime(394):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
E/AndroidRuntime(394):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
E/AndroidRuntime(394):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
E/AndroidRuntime(394):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
E/AndroidRuntime(394):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
E/AndroidRuntime(394):  at java.lang.Thread.run(Thread.java:1019)

E/AndroidRuntime(394): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

E/AndroidRuntime(394):  at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
E/AndroidRuntime(394):  at android.view.ViewRoot.requestLayout(ViewRoot.java:629)
E/AndroidRuntime(394):  at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394):  at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394):  at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394):  at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394):  at android.widget.ScrollView.requestLayout(ScrollView.java:1291)
E/AndroidRuntime(394):  at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394):  at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
E/AndroidRuntime(394):  at android.view.View.requestLayout(View.java:8267)
E/AndroidRuntime(394):  at android.widget.TextView.checkForRelayout(TextView.java:5521)
E/AndroidRuntime(394):  at android.widget.TextView.setText(TextView.java:2724)
E/AndroidRuntime(394):  at android.widget.TextView.setText(TextView.java:2592)
E/AndroidRuntime(394):  at android.widget.TextView.setText(TextView.java:2567)
E/AndroidRuntime(394):  at com.MasterZangetsu.kentrocksoc.nextEvent$eventupdate.doInBackground(nextEvent.java:81)
E/AndroidRuntime(394):  at com.MasterZangetsu.kentrocksoc.nextEvent$eventupdate.doInBackground(nextEvent.java:1)
E/AndroidRuntime(394):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
E/AndroidRuntime(394):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)

1 个答案:

答案 0 :(得分:5)

这一行

 eventText.setText("" + HTML);

导致您的问题。您无法从UI更新doInBackground()。它必须在其他方法之一中完成,或者发送回主Activity方法。您可以在onPostExecute()

中执行此操作

Read through the AyncTask Docs

有不同的方法可以完成此操作,但您可以更改AsyncTask以将结果返回onPostExecute()

public class eventupdate extends AsyncTask<String, Void, String> // change last param

然后添加onPostExecute()方法并更改return

中的doInBackground()语句
public class eventupdate extends AsyncTask<String, Void, String>

 @Override
 protected void onPostExecute(String result) {
        super.onPostExecute(result);
         eventText.setText("" + result);
}

 @Override
    protected String doInBackground(String... url) {
   // do your work
   return HTML;