Android的,使用一个异步任务,抓住从URL文本 [英] Android, Using an Async task to grab text from url

查看:99
本文介绍了Android的,使用一个异步任务,抓住从URL文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

右键我想尽可能这正好你应该考虑我一个完整的noob,我在路在我的头上。

我试图凑齐我了解这个了来自每处理不同方面的几个部分教程,我想要做的。

长期和短期的它,我想要做的就是文字从URL文本视图中显示看,我的理解是,我需要的AsyncTask做到这一点,因为3.0的更新。

这是我要去的地方错将是巨大的,因为我不明白是什么导致错误,以及如何解决任何帮助。

我知道,包含doInBackground的try和catch内code运行在拥有3.0更新之前。这是否也需要进行修改与异步的工作,但是我不知道。

我也想像,可能会出现的其他问题,一旦这个人是排序的,如果任何人察觉的东西昭然若揭我将不胜感激,如果你能指出来。

 字符串事件;
TextView的eventText;
TextView的titleText;字符串HTML;公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.newevent);    titleText =(的TextView)findViewById(R.id.Title);
    eventText =(的TextView)findViewById(R.id.Event);    新eventupdate()执行();
}公共类eventupdate扩展的AsyncTask<弦乐,太虚,太虚> {    @覆盖
    保护无效doInBackground(字符串... URL){        尝试{            视频下载(4000);            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpContext的localContext =新BasicHttpContext();
            HTTPGET HTTPGET =新HTTPGET(
                    http://masterzangetsu.eu/Apps/rocksoctest); // URL!
            HTT presponse响应= httpClient.execute(HTTPGET,
                    localContext);
            字符串结果=;            读者的BufferedReader =新的BufferedReader(
                    新的InputStreamReader(response.getEntity()的getContent()));            串线= NULL;
            而((行= reader.readLine())!= NULL){
                结果+ =行+\\ n;
                HTML =结果;
            }        }赶上(InterruptedException的E){            e.printStackTrace();
        }赶上(ClientProtocolException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }        eventText.setText(+ HTML);        // TODO自动生成方法存根
        返回null;
    }
}

而LogCat中的错误是:

  W / dalvikvm(394):主题ID = 9:螺纹未捕获的异常(组= 0x40015560)退出
E / AndroidRuntime(394):致命异常:AsyncTask的#1
E / AndroidRuntime(394):了java.lang.RuntimeException:执行doInBackground发生错误()
E / AndroidRuntime(394):在android.os.AsyncTask $ 3.done(AsyncTask.java:200)
E / AndroidRuntime(394):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:274)
E / AndroidRuntime(394):在java.util.concurrent.FutureTask.setException(FutureTask.java:125)
E / AndroidRuntime(394):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:308)
E / AndroidRuntime(394):在java.util.concurrent.FutureTask.run(FutureTask.java:138)
E / AndroidRuntime(394):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
E / AndroidRuntime(394):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:581)
E / AndroidRuntime(394):在java.lang.Thread.run(Thread.java:1019)E / AndroidRuntime(394):$ android.view.ViewRoot CalledFromWrongThreadException:产生的原因只有创建视图层次可以触摸其观点原来的线程。E / AndroidRuntime(394):在android.view.ViewRoot.checkThread(ViewRoot.java:2932)
E / AndroidRuntime(394):在android.view.ViewRoot.requestLayout(ViewRoot.java:629)
E / AndroidRuntime(394):在android.view.View.requestLayout(View.java:8267)
E / AndroidRuntime(394):在android.view.View.requestLayout(View.java:8267)
E / AndroidRuntime(394):在android.view.View.requestLayout(View.java:8267)
E / AndroidRuntime(394):在android.view.View.requestLayout(View.java:8267)
E / AndroidRuntime(394):在android.widget.ScrollView.requestLayout(ScrollView.java:1291)
E / AndroidRuntime(394):在android.view.View.requestLayout(View.java:8267)
E / AndroidRuntime(394):在android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
E / AndroidRuntime(394):在android.view.View.requestLayout(View.java:8267)
E / AndroidRuntime(394):在android.widget.TextView.checkForRelayout(TextView.java:5521)
E / AndroidRuntime(394):在android.widget.TextView.setText(TextView.java:2724)
E / AndroidRuntime(394):在android.widget.TextView.setText(TextView.java:2592)
E / AndroidRuntime(394):在android.widget.TextView.setText(TextView.java:2567)
E / AndroidRuntime(394):在com.MasterZangetsu.kentrocksoc.nextEvent $ eventupdate.doInBackground(nextEvent.java:81)
E / AndroidRuntime(394):在com.MasterZangetsu.kentrocksoc.nextEvent $ eventupdate.doInBackground(nextEvent.java:1)
E / AndroidRuntime(394):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)
E / AndroidRuntime(394):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:306)


解决方案

下面这行

  eventText.setText(+ HTML);

是造成你的问题。你不能更新 UI doInBackground()。它拥有的其他方法之一做或送回主活动方法。在这里,你可以在做 onPostExecute()

通读AyncTask文档

有完成这个不同的方式,但你可以改变你的的AsyncTask 来结果返回到 onPostExecute()

 公共类eventupdate扩展的AsyncTask<弦乐,太虚,字符串> //改变过去的参数

然后添加 onPostExecute()方法和变化收益语句 doInBackground()

 公共类eventupdate扩展的AsyncTask<弦乐,太虚,字符串> @覆盖
 保护无效onPostExecute(字符串结果){
        super.onPostExecute(结果);
         eventText.setText(+结果);
} @覆盖
    保护字符串doInBackground(字符串... URL){
   // 做你的工作
   返回HTML;

Right I think as far as this goes you should consider me a complete noob, I am in way over my head.

I have attempted to cobble what I understand about this together from several partial tutorials each dealing with a different aspect that I want to do.

Long and short of it, all I want to do is get text read from a URL to display in a text view, my understanding is that I need an AsyncTask to do that since the 3.0 update.

Any help on where I am going wrong would be great as I don't understand what's causing the error and how to fix.

I do know that the code contained within the try and catch of the doInBackground works on its own before the 3.0 update. Whether this too needs to be modified to work with an Async however I don't know.

I also imagine that other issues will probably arise once this one is sorted, if anyone spots something glaringly obvious I would be grateful if you could point it out.

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


}

And the LogCat error is:

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)

解决方案

This line here

 eventText.setText("" + HTML);

is causing your problem. You can't update the UI from doInBackground(). It has to be done in one of the other methods or sent back to a main Activity method. Here you could do it in onPostExecute()

Read through the AyncTask Docs

There are different ways of accomplishing this but you could change your AsyncTask to return result to onPostExecute()

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

then add onPostExecute() method and change return statement in 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;

这篇关于Android的,使用一个异步任务,抓住从URL文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆