How to do a HTTP GET request on a extension? and return the result.
Please include snippets or sample code so I can understand
How to do a HTTP GET request on a extension? and return the result.
Please include snippets or sample code so I can understand
see source of the web component
Taifun
Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.
How can I do it with OkHttp?
You need learn okhttp @Nisarga_Adhikary
Dont worry i make simple extension code to learn together
SimpleOkHttp link repo
im write use browser chrome android not an idea,so maybe need fix
and maybe need add library "okhttp.jar" is com.squareup.okhttp3 library.
regards,
kangris
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
}
}
});
what packages should i import?
Import these classes
java.io.BufferedReader
java.io.IOException
android.app.Activity
java.io.InputStreamReader
java.net.HttpURLConnection
java.net.URL
com.google.appinventor.components.runtime.util.AsynchUtil
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
Learn More: https://www.geeksforgeeks.org/java-net-httpurlconnection-class-java/
Yes
HttpUrlConnection is a child and abstract class which extends UrlConnection and is more secure.
If you want more security then use HttpsUrlConnection.
Will HttpURLConnection
work with those urls whoch has scheme https
Yes.
But HttpsUrlConnection
will not work with http
urls.
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
What have you tried yet ? Why not use web
component ?
No , I want to make extensions
Show what have you tried ?
You can use this example as help :
Request headers too , When I messaged you in telegram then you said me to learn java @oseamiya
Please also tell
@Override
Tell what @vknow360 told
Ah, Okay !