【问题标题】:Exception when connecting to a URL with jsoup使用 jsoup 连接到 URL 时出现异常
【发布时间】:2015-01-18 17:50:26
【问题描述】:

这是我的调试堆栈:

Thread [<1> main] (Suspended (exception IllegalArgumentException))  
<VM does not provide monitor information>   
HttpConnection.connect(String) line: 30 
Jsoup.connect(String) line: 73  
Parsing.doInBackground(String...) line: 23  
MainActivity$1.onClick(View) line: 29   
Button(View).performClick() line: 4424  
View$PerformClick.run() line: 18383 
Handler.handleCallback(Message) line: 733   
ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) line: 95 
Looper.loop() line: 137 
ActivityThread.main(String[]) line: 4998    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 515  
ZygoteInit$MethodAndArgsCaller.run() line: 777  
ZygoteInit.main(String[]) line: 593 

这是我的代码:

package com.example.pricetracker;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import android.os.AsyncTask;


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


    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        Document htmlFile = null; 
        Element price = null;

          try {

               htmlFile = Jsoup.connect("www.amazon.in/Google-Nexus-D821-16GB-Black/dp/B00GC1J55C/ref=sr_1_1?s=electronics&amp;ie=UTF8&amp;qid=1421161258&amp;sr=1-1&amp;keywords=Google").get();
          } catch (IOException ex) {
              // TODO Auto-generated catch block
              ex.printStackTrace();
          }         
          catch (Exception ex) {
              // TODO Auto-generated catch block
              ex.printStackTrace();
          }         

        try{

          if( htmlFile.getElementById("priceblock_ourprice")!= null)
          {
            price = htmlFile.getElementById("priceblock_ourprice");
          }
          else
          {
            price = htmlFile.getElementById("priceblock_saleprice");

          }   
        }catch(Exception e)
          {
            e.printStackTrace();
          }

        return (String)price.toString();

    }

如何从 URL 中获取数据?

这个问题的原因是什么,我该如何解决?

我被困在这里超过一天。

大家有什么想法吗?提前致谢。

【问题讨论】:

    标签: java android eclipse exception jsoup


    【解决方案1】:

    尝试使用同时指定方案的 URL(http 或 https):

    htmlFile = Jsoup.connect("http://www.amazon.in/Google-Nexus-D821-16GB-Black/dp/B00GC1J55C/ref=sr_1_1?s=electronics&amp;ie=UTF8&amp;qid=1421161258&amp;sr=1-1&amp;keywords=Google").get();
                             ^^^^^^^^
    

    编辑

    来自JavaDoc for JSoup.connect(String)

    > url - URL to connect to. The protocol must be http or https.
    

    【讨论】:

    • 嗨@Todd,我检查了API - jsoup.org/apidocs 并没有找到任何这样的方法。我错过了什么吗?
    • 哦!我以前没明白。我添加了“http://”,现在遇到了一个不同的异常。我将编辑问题。请看一下。谢谢@Todd。
    • 最好添加一个新问题,否则在事后诸葛亮/上下文中这些都没有意义。此外,您最好先尝试自己修复它,这就是我们的学习方式。
    • 好的!我恢复了更改并恢复了问题的原始状态。
    • 这里是提出下一个异常的问题 - stackoverflow.com/questions/28013394/… 请看托德! :)
    猜你喜欢
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多