Android https get html

Technology Tutorials

There are 2 different ways to get html content from android webview.

1) Using Javascript
In this approach, url will be loaded by webview itself. Means each and every url will be invoked in the webview context. so webview will manage cookies and http session automatically.

2) Using Java only to get html content from webview.
In this approach, webview will not be responsible for fetching the content from remote servers. We have to handle invocation of urls and have to fetch html content using code and then we can perform whatever operations we require on content before setting it in the webview.
So maintaining cookies and http session will be our responsibility.
In the example snippet below for this approach will show a workaround for maintaining http session.

First you have to configure javascript interface for webview, using below method

webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer"); 

Here 1st parameter is the MyJavaScriptInterface class, that you have to write as below.
and 2nd parameter is the name of the javascript object you will use to call the method you write (showHTML).

class MyJavaScriptInterface  private Context ctx; MyJavaScriptInterface(Context ctx)  this.ctx = ctx; > @JavascriptInterface public void showHTML(String html)  //code to use html content here handlerForJavascriptInterface.post(new Runnable()  @Override public void run()  Toast toast = Toast.makeText(this, "Page has been loaded in webview. html content :"+html_, Toast.LENGTH_LONG); toast.show(); >>); > > 

Note that annotation ‘ @JavascriptInterface’ on the showHTML method. it is mandatory to work this propertly for android >=4.2.
Another thing to keep in mind is that, from showHTML method, you will not able to access other components of your activity directly. You have to post runnable objects to a handler. and write all the logic to access activity’s components inside the run method of runnable.

Now everyting is setup. You just have to call showHTML method explicitly when the page loading is finished into the webview.
For this you have to implement WebviewClient which will be notified by android once page is loaded into the webview, and from that you can invoke showHTML method using javascript and pass the html content loaded into webview.
This can be achieved using below code.

webview.setWebViewClient(new WebViewClient()  @Override public void onPageFinished(WebView view, String url)  webview.loadUrl("javascript:window.HtmlViewer.showHTML" + "(''+document.getElementsByTagName('html')[0].innerHTML+'');"); > >); 

Below is the complete activity code you can refer to if anything is missed in above code snippet.

public class TestActivity extends Activity  Handler handlerForJavascriptInterface = new Handler(); @Override protected void onCreate(Bundle savedInstanceState)  super.onCreate(savedInstanceState); setContentView(R.layout.webview); final WebView webview = (WebView) findViewById(R.id.browser); webview.getSettings().setJavaScriptEnabled(true); webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer"); webview.setWebViewClient(new WebViewClient()  @Override public void onPageFinished(WebView view, String url)  webview.loadUrl("javascript:window.HtmlViewer.showHTML" + "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');"); > > ); webview.loadUrl("http://android-in-action.com/index.php?post/" + "Common-errors-and-bugs-and-how-to-solve-avoid-them"); > class MyJavaScriptInterface  private Context ctx; MyJavaScriptInterface(Context ctx)  this.ctx = ctx; > public void showHTML(String html)  final html_ = html; handlerForJavascriptInterface.post(new Runnable()  @Override public void run()  Toast toast = Toast.makeText(this, "Page has been loaded in webview. html content :"+html_, Toast.LENGTH_LONG); toast.show(); > > ); > > > 

If you are using proguard while exporting your apps then following properties must be included in proguard-project.txt (proguard config file) .

-keepattributes JavascriptInterface -keep public class com.mypackage.MyClass$MyJavaScriptInterface -keep public class * implements com.mypackage.MyClass$MyJavaScriptInterface -keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface  methods>; > 

2) Using Java only to get html content from webview.

In this approach, you have to handle page loading events (page loading started, page loading finished) of webview based on your requirements and from there you have to use httpclient api to actually hit the url and get the content.
this way you will have the access to the html content returned by remote server and you can modify the content as per your needs before/after it is rendered into the webview.

Below is the complete code for this.

import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; /** * getting html content from webview using java only. not using javascript for * that. */ public class WebviewTestActivity1 extends Activity  @Override protected void onCreate(Bundle savedInstanceState)  super.onCreate(savedInstanceState); setContentView(R.layout.activity_webview_test_activity1); final WebView webview = (WebView) findViewById(R.id.webview1); webview.getSettings().setJavaScriptEnabled(true); WebViewClient webViewClient = new WebViewClient()  /** * this method will be invoked when page started loading into the * webview. so you can use thid method if you want to modifiy the * content before it is rendered. */ @Override public void onPageStarted(WebView view, String url, Bitmap favicon)  super.onPageStarted(view, url, favicon); String htmlContent = getRemoteContent(url); webview.loadDataWithBaseURL(url, htmlContent, null, "utf-8", url); > /** * this method will be invoked when page loading is finished into * the webview. */ @Override public void onPageFinished(WebView view, String url)  // TODO Auto-generated method stub super.onPageFinished(view, url);
// similar code as above method will go here . > >; webview.setWebViewClient(webViewClient); > /** * this method will hit the remote url and get the content. this content * will be set in the webview. */ private String getRemoteContent(String url)  HttpGet pageGet = new HttpGet(url); HttpClient client = new DefaultHttpClient(); ResponseHandlerString> handler = new ResponseHandlerString>()  public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException  HttpEntity entity = response.getEntity(); String html; if (entity != null)  html = EntityUtils.toString(entity); return html; > else  return null; > > >; String pageHTML = null; try  pageHTML = client.execute(pageGet, handler); //if you want to manage http sessions then you have to add localContext as a third argument to this method and have uncomment below line to sync cookies. //syncCookies(); > catch (Exception e)  e.printStackTrace(); > // you can filter your html content here if you wish before displaying // in webview return pageHTML; > > 

As stated above, this approach will not maintain cookies and http sessions.

So if you don’t have requirement to maintain http session, then above code snippet is sufficient. Otherwise you have to implement additional code to achieve session management.
This is how you can achieve it.

First thing is you have to make your httpclient instance global. Means, you have to use same http client for executing each and every request.
Also you have to create global HttpContext and CookieStore objects, which will be used for executing http requests.

You can use below method to initialize the httpclient.

private static DefaultHttpClient httpClient = null; private static void initHttpClient()  httpClient = new DefaultHttpClient(); ClientConnectionManager cm = httpClient.getConnectionManager(); SchemeRegistry sr = cm.getSchemeRegistry(); sr.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); sr.register(new Scheme("https", TrustAllSSLSocketFactory.getSocketFactory(), 443)); cookieStore = new BasicCookieStore(); localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); > 

Now you have to execute each and every request in localContext, which is using cookieStore internally.
And after invoking the request urls, you have to call below method to sync the cookies with webview.

private void syncCookies()  Cookie sessionInfo; ListCookie> cookies = client.getCookieStore().getCookies(); Log.d(TAG, "cookies background-color: white;"> + cookies); if (!cookies.isEmpty())  WebView webView = (WebView) findViewById(R.id.webView1); CookieSyncManager.createInstance(webView.getContext()); CookieManager cookieManager = CookieManager.getInstance(); for (Cookie cookie : cookies)  sessionInfo = cookie; String cookieString = sessionInfo.getName() + " background-color: white;"> + sessionInfo.getValue() + "; domain background-color: white;"> + sessionInfo.getDomain(); Log.d(TAG, "cookieString background-color: white;"> + cookieString); cookieManager.setCookie("your_host_address_here>", cookieString); CookieSyncManager.getInstance().sync(); > > > 

Thats it! Feel free to add your feedback/comments if there are any.

Источник

Android https get html

I’m trying to get an HTML resource from a website on Android.

The following site is obtained using HttpURLConnection.

Basically, we have confirmed that we can get HTML resources.
(For sites such as»Google»and»Yahoo»)
However, there are some sites that cannot get resources.
For example
Apple Developer site (https://developer.apple.com/news/)
is. Try to get
javax.net.ssl.SSLException: SSL handshake aborted: ssl = 0x5c73e618: I/O error during system call, Connection reset by peer
I get an error.

If i can’t get it with HttpURLConnection, you can get it from WebView.
I tried to cope with the following implementation.

This too was not possible on the above Apple Developer site. Even WebView is not displayed in the first place.

Au’s «https://auone.jp» can be obtained, but the Apple Developer site is not available.

It may be necessary to change the User-Agent, but that alone is not enough.

Can you tell me how to get it?

I’m not sure if this is related to this phenomenon. . . https://developer.apple.com/news/ seems to allow only TLS1.2
If you have Android 4.3 or lower, you will not be able to connect to https (with standard browser).

Can I use HttpsURLConnection? You can also refer to https://developer.android.com/training/articles/security-ssl.html?hl=en#CommonProblems.

  • java — android sessiongetinstance () error [send email]
  • java — socket communication in android studio → i want to transfer images
  • i want to build an android application from ren’py launcher, but i am unable to proceed with an error (java compilation error)
  • java — [android] i want to insert two images inside with a circle frame image as the background [xml]
  • java — [android studio] switching to dark mode how to specify multiple textcolors in stylesxml
  • java — okhttp cannot be imported in android
  • java — i want to create a firestore timestamp in android studio
  • java — i want to play youtube on android using the youtube android player api
  • java — unable to launch emulator in android studio
  • java — about library introduction in android studio
  • java — [android] i want to display characters on two textviews with a time difference
  • java — i cannot use handler of android well
  • java — i want to reflect the data selected with android studio spinner in the textview
  • java — when i try to run it in android studio, i get layout_constrainend_toendof is not found
  • java — how to distinguish «android studio» library or homebrew function, create button
  • java — i want to run main on android stuido
  • java — i get an error in android build error: can’t find symbol
  • java — about android socket communication
  • java — how to keep android studio images
  • java — [android] i want to get colors as a character string from colorsxml change the color of vector image «setcolorfilter

Источник

Читайте также:  Html draw horizontal line
Оцените статью