【问题标题】:Webview inside AsynctaskAsynctask 中的 Webview
【发布时间】:2016-11-06 06:32:07
【问题描述】:
public class SecondActivity2 extends AppCompatActivity {

    public static String main_html = "";
    public static int second_action = 0;
    WebView webView2;
    public static taskResearch ndc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        webView2 = (WebView) findViewById(R.id.webView);

        String userid = "12345";

        try {
            ndc = new TaskResearch(userid, "1", "keyword", 5, 4, "orderid", 6, "title", "text");
            ndc.execute("");
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.v("htmlg", "taskResearch finished");

    }



    class TaskResearch  extends AsyncTask <String, String, String> {

        String deviceId, s_id, keyword, city, s_title, s_text;
        int count1, size1, online;

        public TaskResearch(String deviceId, String s_id, String keyword, int count1, int size1, String city, int online, String s_title, String s_text){

            this.deviceId = deviceId;
            this.s_id = s_id;
            this.keyword = keyword;
            this.city = city;
            this.s_title = s_title;
            this.s_text = s_text;
            this.count1 = count1;
            this.size1 = size1;
            this.online = online;

        }

        protected String doInBackground(String... strings) {

            String sonuc = "";

            runOnUiThread(new Runnable() {
                public void run() {





                }
            });

            return "a";

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }


    }

    class html_reader {

        @SuppressWarnings("unused")
        @JavascriptInterface
        public void processHTML(final String html) {

            // Log.d("HTMLG", "html: " + html);

            // Log.d("HTMLG", "html: " + html);
            // html_reader jssil = new html_reader(deviceId, sId,  swAfter, swcity);

            // webView2.addJavascriptInterface(jssil, "HTMLOUT");

            main_html = html;

        }

        @SuppressWarnings("unused")
        @JavascriptInterface
        public void ok() {

            // Log.d("HTMLG", "html: " + html);

            // Log.d("HTMLG", "html: " + html);
            // html_reader jssil = new html_reader(deviceId, sId,  swAfter, swcity);

            // webView2.addJavascriptInterface(jssil, "HTMLOUT");

            second_action = 1;

        }


}

朋友们好。

我的代码在上面。我想运行这个命令。

ndc = new taskResearch(userid, "1", "keyword", 5, 4, "orderid", 6, "title", "text");

当这个命令完成时。我想让这个命令运行。

Log.v("htmlg", "taskResearch finished");

但是其中两个一起开始,第二个首先完成。我希望第一个命令应该开始并完成,然后第二个命令应该开始。我怎样才能做到这一点 ?

我的错误是什么?

【问题讨论】:

标签: java android android-studio webview android-asynctask


【解决方案1】:

顺便说一句,如果您从网站中提取 html 代码,您可以使用不同的结构。

您可以使用 onPostExecute 来检测进程结束。

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    processFinished();
}


void processFinished(){
   Log.v("htmlg", "task_research finished");
}

【讨论】:

  • @HakanSarı 在您的代码中,您在 super.onPostExecute(s) 之后没有日志语句。添加并重试。
【解决方案2】:

一种方法是覆盖 onPostExecute 方法,如另一篇文章中所述。另一种方法如下:

调用 ndc.execute() 后,您可以等待它得到如下结果:

String output = ndc.get(); // i believe this is the api

一旦你返回那个输出,你可以做任何你想做的事情,第二个日志将在之后打印。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多