How to do a HTTP GET request on a extension?

Actually, it's easy. Still, I recommend you to look at what @Taifun said.

Here is the code which I use. With simple explanation:

 @SimpleFunction(
            description = "Website source-code")
    public void WebsiteContent(final String website) {
        AsynchUtil.runAsynchronously(new Runnable() { // Always do get request Asynchronously
            @Override
            public void run() {
                try {
                    BufferedReader readStream = new BufferedReader(
                            new InputStreamReader(
                                    new URL(website).openStream())); // Open stream and read the content from streamReader to BufferedReader
                    String readLine; // Variable which will have one line of data
                    StringBuilder data = new StringBuilder(); // The result data will be stored here
                    while ((readLine = readStream.readLine()) != null) data.append(readLine); // Read all the lines from the readStream
                    readStream.close(); // IMPORTANT close the stream

                    final String finalData = data.toString(); // Make the string final with the data variable
                    activity.runOnUiThread(new Runnable() { // Always raise events on UI thread
                        @Override
                        public void run() {
                            myEvent(finalData); // You're event with data
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace(); // Error occured
                }
            }
        });
8 Likes