w3stling / rssreader

A simple Java library for reading RSS and Atom feeds
MIT License
152 stars 25 forks source link

Freeze on Android Device #189

Open kevlahnota opened 1 month ago

kevlahnota commented 1 month ago

I assume there are classes that needs to be retained on Proguard.cfg hence the freeze issue (are there any information so It works on android). Tested using Android 14 (SDK 34) compiled using Java 17 and rssreader v3.6.0 (the build tools complains using java 21 so I use lower version). The java file is this:

import com.apptasticsoftware.rssreader.Item;
import com.apptasticsoftware.rssreader.RssReader;
import org.apache.commons.text.StringEscapeUtils;

import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

public class RSS {
    private static RssReader reader;
    private static RssReader getReader() {
        if (reader == null)
            reader = new RssReader();
        return reader;
    }

    public static String getLog(Date buildDateOriginal, SimpleDateFormat DateFor, Date max) {
        String message = "";
        try {
            URL url = new URL("https://github.com/Card-Forge/forge/commits/master.atom");
            InputStream inputStream = url.openStream();
            List<Item> items = getReader().read(inputStream).toList();
            StringBuilder logs = new StringBuilder();
            int c = 0;
            for (Item i : items) {
                if (i.getTitle().isEmpty())
                    continue;
                String title = TextUtil.stripNonValidXMLCharacters(i.getTitle().get());
                if (title.contains("Merge"))
                    continue;
                ZonedDateTime zonedDateTime = i.getPubDateZonedDateTime().isPresent() ? i.getPubDateZonedDateTime().get() : null;
                if (zonedDateTime == null)
                    continue;
                Date feedDate = Date.from(zonedDateTime.toInstant());
                if (buildDateOriginal != null && feedDate.before(buildDateOriginal))
                    continue;
                if (max != null && feedDate.after(max))
                    continue;
                logs.append(DateFor.format(feedDate)).append(" | ").append(StringEscapeUtils.unescapeXml(title).replace("\n", "").replace("        ", "")).append("\n\n");
                if (c >= 15)
                    break;
                c++;
            }
            if (logs.length() > 0)
                message += ("\n\nLatest Changes:\n\n" + logs);
            inputStream.close();
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return message;
    }
}

It works fine on desktop version though.

w3stling commented 1 month ago

The RssReader library relies on classes not supported in the Android platform.

XML Parsing: It uses javax.xml.stream.XMLStreamReader (StAX API), which is not directly available on Android. But an Android compatible StAX implementation like Woodstox can be used.

HTTP Requests: It uses java.net.http.HttpClient, which is also not available on Android. It can be replaced with a 3rd-party HTTP client library designed for Android (e.g., OkHttp).

kevlahnota commented 1 month ago

The RssReader library relies on classes not supported in the Android platform.

XML Parsing: It uses javax.xml.stream.XMLStreamReader (StAX API), which is not directly available on Android. But an Android compatible StAX implementation like Woodstox can be used.

HTTP Requests: It uses java.net.http.HttpClient, which is also not available on Android. It can be replaced with a 3rd-party HTTP client library designed for Android (e.g., OkHttp).

Thanks for pointing it out, gonna try if importing those dependency will work.

w3stling commented 1 month ago

I got it working on Android. It's a temporary solution that doesn't require any code changes to the library. I will improve Android support in a future version.

Add dependencies:

implementation 'com.fasterxml.woodstox:woodstox-core:7.0.0'
implementation 'com.apptasticsoftware:rssreader:3.8.2'

Create class java.net.http.HttpClient

package java.net.http;

public class HttpClient {
}

Provide an instance of HttpClient when creating a new RssReader object.

URL url = new URL("https://github.com/Card-Forge/forge/commits/master.atom");
InputStream inputStream = url.openStream();
List<Item> items = new RssReader(new HttpClient()).read(inputStream).toList()

This solution is limited to reading a feed from an input stream.

kevlahnota commented 1 month ago

Ok thanks, gonna try that on the project.