【问题标题】:Dynamic Link crawl using Retrofit2使用 Retrofit2 进行动态链接抓取
【发布时间】:2017-01-11 07:53:58
【问题描述】:

我正在实现 WhatsApp 之类的链接预览功能,即

  1. 提供任何链接,获取其所有 Html
  2. 通过 Html 爬取,读取所有信息
  3. 显示文字和图片

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


    【解决方案1】:

    不幸的是,您不能以这种方式在运行时更改 Retrofit 中的 URL。

    试试这个教程:https://futurestud.io/tutorials/retrofit-2-how-to-change-api-base-url-at-runtime-2

    或者这个答案:Set dynamic base url using Retrofit 2.0 and Dagger 2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 2015-02-27
      • 2021-03-09
      相关资源
      最近更新 更多