【发布时间】:2017-01-11 07:53:58
【问题描述】:
我正在实现 WhatsApp 之类的链接预览功能,即
- 提供任何链接,获取其所有 Html
- 通过 Html 爬取,读取所有信息
- 显示文字和图片
Jsoup 库
我可以使用 Jsoup 库成功地执行此操作
Document doc = Jsoup.connect("http://www.techjuice.pk").userAgent("Mozilla").get();
它返回页面的html代码作为响应。
改造库
现在我想使用 Retrofit 执行相同的任务
Retrofit retrofit = new Retrofit.Builder()
.build();
API api = retrofit.create(API.class);
Call<ResponseBody> call = api.crawlLink("http://techjuice.pk");
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
API.class
public interface API {
@GET
Call<ResponseBody> crawlLink(@Url String url);
}
例外
java.lang.IllegalStateException:需要基本 URL。
【问题讨论】:
标签: android web-crawler jsoup retrofit2 whatsapp