使用 jsoup 连接到 URL 时出现异常
Exception when connecting to a URL with jsoup
这是我的调试堆栈:
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.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
ing
这是我的代码:
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&ie=UTF8&qid=1421161258&sr=1-1&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获取数据?
这个问题的原因是什么,我该如何解决?
我被困在这里一天多了。
大家有什么想法吗?提前致谢。
尝试使用也指定方案(http 或 https)的 URL:
htmlFile = Jsoup.connect("http://www.amazon.in/Google-Nexus-D821-16GB-Black/dp/B00GC1J55C/ref=sr_1_1?s=electronics&ie=UTF8&qid=1421161258&sr=1-1&keywords=Google").get();
^^^^^^^^
编辑
来自JavaDoc for JSoup.connect(String):
> url - URL to connect to. The protocol must be http or https.
这是我的调试堆栈:
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.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
ing
这是我的代码:
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&ie=UTF8&qid=1421161258&sr=1-1&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获取数据?
这个问题的原因是什么,我该如何解决?
我被困在这里一天多了。
大家有什么想法吗?提前致谢。
尝试使用也指定方案(http 或 https)的 URL:
htmlFile = Jsoup.connect("http://www.amazon.in/Google-Nexus-D821-16GB-Black/dp/B00GC1J55C/ref=sr_1_1?s=electronics&ie=UTF8&qid=1421161258&sr=1-1&keywords=Google").get();
^^^^^^^^
编辑
来自JavaDoc for JSoup.connect(String):
> url - URL to connect to. The protocol must be http or https.