【问题标题】:How to use Jsoup to get web content from an API如何使用 Jsoup 从 API 获取网页内容
【发布时间】:2020-01-21 18:47:23
【问题描述】:

我有一个 API,其响应位于 JSON,我需要从中获取一些内容,其中之一是来自各种新闻网站的 url。我希望从各种新闻网站 (url) 中获取各个段落,但我遇到了困难,因为将 urlJSON 传递到 Jsoup,因为 string 无法正常工作。

我尝试使用其他 url,但我注意到它们不起作用示例:使用 www.google.com 不起作用,但将 https:// 添加到 www.google.com 有效。

请问有没有人知道如何解决这个错误。

package wami.ikechukwu.kanu;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class news_detail extends AppCompatActivity {

    //TODO: REMOVE THESE LINE IF THERE IS NO NEED FOR THEM IN THE APP
    // private final String KEY_AUTHOR = "author";
    private final String KEY_TITLE = "title";
    //private final String KEY_DESCRIPTION = "description";
    private final String KEY_URL = "url";
    private final String KEY_URL_TO_IMAGE = "urlToImage";
    private final String KEY_PUBLISHED_AT = "publishedAt";
    int itemPosition;
    //this string is appended to the url
    String urlLink = "buhari";
    String url;
    TextView newsDetail_Title, newDetail_Time_Posted, newsDetail_News;
    ImageView newsDetail_Image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);

        itemPosition = getIntent().getIntExtra("POSITION", 0);

        newsDetail_Title = findViewById(R.id.newsDetail_Title);
        newDetail_Time_Posted = findViewById(R.id.newDetail_Time_Posted);
        newsDetail_News = findViewById(R.id.newsDetail_News);
        newsDetail_Image = findViewById(R.id.newsDetail_Image);

        newsRequest();
    }

    public void newsRequest() {

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "https://newsapi.org/v2/everything?q=" + urlLink + "&language=en&sortBy=publishedAt&pageSize=100&apiKey=a5f976b34089493abc8f97f088e5df64", null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {

                try {
                    JSONArray jsonArray = response.getJSONArray("articles");

                    //Using a for loop to get the object (data) in the JSON
                    JSONObject jsonObject = jsonArray.getJSONObject(itemPosition);

                    newsDetail_Title.setText(jsonObject.getString(KEY_TITLE));
                    url = jsonObject.getString(KEY_URL);
                    Glide.with(getApplicationContext()).load(jsonObject.getString(KEY_URL_TO_IMAGE)).into(newsDetail_Image);

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

                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonObjectRequest);

        new Thread(new Runnable() {

            final StringBuilder builder = new StringBuilder();
            String title;

            @Override
            public void run() {

                try {
                    Document document = Jsoup.connect(url).get();
                    Elements element = document.select("p");
                    for (Element paragraph : element) {
                        builder.append(paragraph.text());
                    }

                    // title = document.title();

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

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        newsDetail_News.setText(builder.toString());
                        // newsDetail_News.setText(title);

                    }
                });
            }
        }).start();
    }

}


【问题讨论】:

    标签: android json api android-studio jsoup


    【解决方案1】:

    是的,Jsoup 会抛出异常是 url 不包含 http 或 https。

    试试这个(在 Kotlin 中),你也可以将代码转换为 java

    previewUrl = if (!url.contains("http")) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
                        "https://$url"
                    else {
                        "http://$url"
                    }
                } else {
                    url
                }
    

    【讨论】:

    • 能否请您解释一下这段代码的作用,以便我可以手动将其转换为 java。
    • 这段代码主要检查url是否包含http。如果它在 url 中不包含 http,那么它会在基于 Android 版本 P 的 url 之前添加 http 或 https。因为在上面的 P 上它只支持 https 调用。如果它包含http,则直接使用它。
    • 尝试处理重定向和超时异常 document= Jsoup.connect(previewUrl).followRedirects(true).timeout(TIMEOUT_MILLIS).get()
    • 谢谢,这可能是 "https://$url" 的 java 等价物。我不知道 kotlin。
    • 字符串 newUrl; if(!url.contains("http")){ if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.P){ newUrl = "https"+ url; }else { newUrl = "http"+ 网址; } }else{ newUrl = 网址; }
    猜你喜欢
    • 2013-02-16
    • 2015-09-14
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多