How to get HTML input in kotlin? - android

I want to read a html site in a string with kotlin.
But I cannot find how I even get the information from the website or which command I need for it.

The following code reads the content of http://www.android.com/ and outputs the text in the log.
val url = URL("http://www.android.com/")
val urlConnection = url.openConnection() as HttpURLConnection
try {
val text = urlConnection.inputStream.bufferedReader().readText()
Log.d("UrlTest", text)
} finally {
urlConnection.disconnect()
}
See https://developer.android.com/reference/java/net/HttpURLConnection on how interact with HTTP servers in Android.
Kotlin provides the extension methods bufferedReader() and readText which make reading streams more convenient.

You can make use of Jsoup library
Jsoup html parser

Related

POST Request on kotlin

I am trying to execute a link so that I can control an ESP32 using Kotlin in Android Studio, but it is unable to execute it the same way a WebView can (I use the WebView to get a video feed, and it is able to transfer the video). The below is my code
Activity
val url = URL(ipcomm2)
val con: HttpURLConnection = url.openConnection() as HttpURLConnection
con.requestMethod = "POST"
WebView
setContentView(webview)
webview.loadUrl(IPfeed)
Can someone explain why this is the case? Thanks.
You need to call getInputStream and read it out.
See https://developer.android.com/reference/java/net/HttpURLConnection

Searching for code to log raw HTTP+cookies from Android HttpURLConnection

Has anyone released code to show the full HTTP request/response headers, any intermediate redirects, and any cookie data for the Android HttpURLConnection? This would be similar to Firefox Web Console
I roughly know how to write this myself, but 1) it's a non-trivial amount of code 2) it's tricky to get this kind of code to work in all instances. So i'm interested in finding a readymade solution. I know how to tcpdump the emulator, but I'm searching for code to print this information into the Android Log class for really quick runtime debugging.
for header fields
URL url = new URL(str_url);
HttpURLConnection conection = (HttpURLConnection) url.openConnection();
conection.setConnectTimeout(TIMEOUT_SOCKET);
conection.setReadTimeout(TIMEOUT_CONNECTION);
conection.addRequestProperty("Accept-Encoding", "gzip");
RedirectLocations locations = new RedirectLocations();
// here u get all header fields and properties write it in logs
conection.getHeaderFields();
conection.getRequestProperties();
// conection.getOutputStream().write(buffer);
// download the file
InputStream is = conection.getInputStream();
// This is file path were a; quiz data will get saved.
// String file_path = context.getDir(folder,Activity.MODE_PRIVATE).getAbsolutePath();
return unzip(is,save_file_path);
for redirects
link
after u get response, again u ve to look for header fields

How to determine URL has valid domain or not or it's a valid url?

What i get in android native messaging, whenever we send a message to some one and in the text there is a url, android recognizes that and underlined it showing it as a link, it does that for many domains, like .us,.uk,.dk,.ch and all others valid.
Even we send jhjh.us without 'www' or 'http' it recognises it as link.
and if the domain is wrong it doesn't do any thing.
I want the same thing, I tried using pattern
(((https?|ftp|file)://)|(www\\.))"+ "[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]
it does a good but in domain it didn't help. Also tried using URLUtil.isValidUrl() but of no use,
Can anyone give me some idea regarding this.
You can try this
public boolean isURL(String url)
{
try {
new URL(url);
return true;
} catch (MalformedURLException e) {
return false;
}
}
U can use the Use UrlValidator to validate the URL
Considering you are using Class UrlValidator
UrlValidator urlValidator = new UrlValidator();
urlValidator.isValid("http://Test Link!");
There are several properties that you can set to control how this class behaves, by default http, https, and ftp are accepted.

Read Buffered stream website and open the first result Android

I have the following code to read a website. The code is searches on google. I want to open automatically the first result and read it again. For the keywords i want the "I am feeling lucky is not working". Please somebody help me find a solution
I have the following code.
URL url = new URL("http://www.google.com/search?q=sample");
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
try {
readStream(in);
finally {
in.close();
}
}
The query sample is just a sample....
Its already discussed in SO QA :
How to search in google by using java code?
Your approach is also a solution. But it will be complex as it requires parsing of returned HTML.
I think there are some APIS for doing this.
http://code.google.com/p/google-api-java-client/
This is deprecated but might still work -->
http://code.google.com/apis/websearch/docs/

How can I use http auth information from a URL in Android?

In my browser, or in iOS, when I try to get the contents of a URL with encoded http authentication information in the form
http://myUser:myPassword#www.example.com/secure/area/index.html
It just works. I'm getting URLs from a web service, and I'd like to avoid trying to parse them up for their HTTP auth info if I can help it. Is there a way to do something similar in Android without actually parsing the URLs? Alternatively, what is the best way to go about that?
UPDATE:
I find that when I try to set the authentication information in an Authorization header, I get a very strange FileNotFoundException.
Here's the code I'm using:
URL url = new URL(urlString);
URLConnection connection;
String authority = url.getAuthority();
if (authority.contains("#")) {
String userPasswordString = authority.split("#")[0];
url = new URL(urlString.replace(userPasswordString + "#", ""));
connection = url.openConnection();
String encoded = new String(Base64.encode(userPasswordString.getBytes(), Base64.DEFAULT), "UTF-8");
connection.setRequestProperty("Authorization", "Basic " + encoded);
} else {
connection = url.openConnection();
}
InputStream responseStream = connection.getInputStream();
All the info seems to check out, I've verified the url is correct, the base64 string is correct, and the file is certainly on the server--I have no trouble at all opening it with Firefox, and Firebug shows all the right headers, matching what I've sent as far as I can tell. What I get though is the following error (url host changed to protect the innocent):
java.io.FileNotFoundException: http://a1b.example.com/grid/uploads/profile/avatar/user1/custom-avatar.jpg
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1061)
Any idea what this is all about?
I looked into using HttpClient, but saw that in Issue 16041 it is recommended that we prefer URLConnection.
That looks like your browser is applying some extra rules to parsing the URL. In Android you can use HTTP Client's authentication mechanism such as BASIC and DIGEST to do the same things. Which one you choose is dependent on the server you are trying to authenticate against.
Here is a good page to get you started.
Unfortunately, on Android you can't pass the user info (username/password) in that format to either java.net.URL or HttpClient and have it work like in a browser.
I'd recommend using URI (see http://download.oracle.com/javase/1.5.0/docs/api/index.html?java/net/URI.html) to do this: pass your URL to the URI constructor that takes a String and then you can extract the user info (using getUserInfo()). You can then either use HttpClient's authorization classes (see http://developer.android.com/reference/org/apache/http/auth/package-summary.html) or build the basic auth header yourself (an example is given at http://www.avajava.com/tutorials/lessons/how-do-i-connect-to-a-url-using-basic-authentication.html).

Categories

Resources