【问题标题】:Jsoup html display in webview not workingWebview中的Jsoup html显示不起作用
【发布时间】:2017-06-16 22:19:40
【问题描述】:

我正在 android studio 中开发一个 android 应用程序,该应用程序从 amazon 中提取一些数据并将其显示在 webview 中。

由于某种原因,webview 保持空白,我已经使用 eclipse 在标准 java 项目中测试了我的代码。 Jsoup 代码很好,在 eclipse 中运行干净,但在 android studio 中也没有显示任何错误,除了没有 webview。

这是我的代码

public class showdata extends AppCompatActivity {
    WebView firstresault;
  public String amazonproduct;
    public Element productamazon;
    public class getres extends AsyncTask<Void,Void,Void> {


        @Override
        protected Void doInBackground(Void... params) {
            try {
                Document doc = Jsoup.connect("https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=841351100182").get();
                productamazon = doc.select("div#s-item-container").first();
                amazonproduct = productamazon.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            WebView wview = (WebView)findViewById(R.id.amazon_product);
            wview.loadData( amazonproduct, "text/html", null);
            Log.i("amazonproduct", "Null or Not");
        }
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showdata);
        new getres();



    }



}

我是 android api 的新手,所以我确信它很简单。感谢所有帮助!

日志猫

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                                          Process: com.example.rober.shoppingappbarcode, PID: 11086
                                                                                          java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                              at android.os.AsyncTask$3.done(AsyncTask.java:318)
                                                                                              at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                              at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                              at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                              at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                                                              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                                              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                                              at java.lang.Thread.run(Thread.java:761)
                                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.jsoup.nodes.Element.toString()' on a null object reference
                                                                                              at com.example.rober.shoppingappbarcode.showdata$getres.doInBackground(showdata.java:28)
                                                                                              at com.example.rober.shoppingappbarcode.showdata$getres.doInBackground(showdata.java:20)
                                                                                              at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                                                              at java.util.concurrent.FutureTask.run(FutureTask.java:237)

【问题讨论】:

    标签: java android eclipse android-studio webview


    【解决方案1】:

    你需要使用execute()函数调用来执行你的AsyncTask

    new getres().execute();
    

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_showdata);
            new getres().execute();
            //           ^^^^^^^
        }
    

    试试这个

    productamazon = doc.getElementsByClass("s-item-container").first();
    

    而不是这个

    productamazon = doc.select("div#s-item-container").first();
    

    【讨论】:

    • 关闭,但我的应用在使用您添加的修复程序启动几秒钟后崩溃
    • @Top-Bot 肯定还有其他问题,请在问题末尾发布 logcat 错误详细信息
    • @Top-Bot 这个doc.select("div#s-item-container").first(); 什么都不返回,意思是null 因此例外
    • 感谢帕夫尼特!你摇滚!
    • @Top-Bot 感谢您的夸奖,我很高兴能帮上忙,祝您编码愉快
    猜你喜欢
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 2012-01-06
    • 2023-03-07
    • 2011-11-10
    • 1970-01-01
    相关资源
    最近更新 更多