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
}
}
});
Although @Kumaraswamy 's approach is good but it will only work for GET requests.
But if you are looking for other options then you should do this instead:
URL obj = new URL("https://www.google.com");
HttpURLConnection httpConnection = (HttpURLConnection)obj.openConnection();
httpConnection.setDoOutput(true); //if you want output
httpConnection.setRequestMethod("POST"); //request method
BufferedReader bufferedReader = null;
if (httpConnection.getResponseCode() == 200) {
bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
} else {
bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));
}
StringBuilder content = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null){
content.append(line).append("\n");
}
bufferedReader.close();
final String con = String.valueOf(content); //post wherever you need
When you open a URL for connection, Java will internally instantiate the right class for the scheme. You can downcast the URLConnection object to the more concrete type appropriate for the scheme, but it isn't necessary unless you need protocol specific functionality (e.g., setting HTTP headers).
Can any gave me the code for making a web , requesting two header and getting responsive content in dispatcher block
I am trying rapid api extensions , so can any one give me one source code of any rapid api requester extension so that I will also begin extension developing
Your baserow source code also include utility.java upload by url , and that make me cunfuse because its mixing in one class extends
I want to know how to do this in one class only
I donot want to use many classes