I’m working on an Android app that needs to do some automated tasks on a website. Right now, I’m using WebView with a Java-JavaScript interface. It works, but it’s not ideal. I have to make the WebView full-screen and invisible in a service to run it in the background.
The main issue is that the website uses WebSockets for requests, so I can’t just use simple GET/POST methods in Java. The website’s JavaScript is also heavily obfuscated, making it hard to figure out the WebSocket API (Chrome debugger only shows ‘Binary OPCode 2’ in the Frames tab).
Is there a better way to do this on Android? I’m looking for a headless browser solution that supports WebSockets. It would make things much easier and cleaner. Any suggestions or alternatives would be really helpful!
Have you considered using Selenium with ChromeDriver for Android? It’s a powerful tool that supports headless browsing and WebSocket connections. I’ve used it in several projects, and it’s been quite reliable.
To set it up, you’ll need to include the Selenium WebDriver and ChromeDriver dependencies in your project. Then, you can create a ChromeDriver instance with headless options enabled. This approach allows you to interact with the website programmatically without rendering a visible browser.
One caveat: running Selenium on Android can be resource-intensive. Make sure to optimize your code and consider the impact on battery life. Also, you might need to handle permissions carefully, especially if your app targets newer Android versions.
If Selenium feels too heavy, you could look into libraries like jsoup combined with OkHttp for WebSocket support. It’s a lighter alternative, though you’d need to handle JavaScript execution separately.
hey, have u tried puppeteer for android? it supports websockets and headless browsing. i used it in a project with no major issues. setup requires node.js though. it’s lightweight, but monitor mem usage on older devices.
I’ve been in a similar situation, and I found that using jsoup in combination with OkHttp worked wonders for my Android project. It’s a lightweight solution that handled WebSockets beautifully.
Here’s what I did: I used jsoup to parse the HTML and extract the necessary information from the website. Then, I set up OkHttp to manage the WebSocket connections. This approach allowed me to interact with the site’s API without dealing with a full browser environment.
One thing to keep in mind is that you might need to reverse-engineer some of the JavaScript to understand the WebSocket communication. It can be tricky with obfuscated code, but tools like Charles Proxy can help you analyze the traffic and figure out the message structure.
Also, make sure to handle connection drops and reconnects gracefully. WebSockets can be finicky on mobile networks. Overall, this setup gave me much better performance and battery life compared to using WebView or Selenium.