Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid app using webview gives html code on button click of url
    primarykey
    data
    text
    <p>I want to develop an android application that would open a url in a webView, using shouldInterceptRequest method in webViewClient. I must get the data using HttpClient. So I built the application but as I run the application, the main.xml gets loaded and then when I click on the button it gives html code of the url.</p> <p>main.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;Button android:id="@+id/buttonurl" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="visit www.stackoverflow.com" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>webview.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webView1" android:layout_width="fill_parent" android:layout_height="fill_parent"/&gt; </code></pre> <p>AndroidManifest.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example" android:versionCode="1" android:versionName="1.0"&gt; &lt;uses-sdk android:minSdkVersion="16"/&gt; &lt;application android:label="@string/app_name" android:icon="@drawable/ic_launcher"&gt; &lt;activity android:name="MyActivity" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN"/&gt; &lt;category android:name="android.intent.category.LAUNCHER"/&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".WebViewActivity" android:label="@string/app_name"&gt; &lt;intent-filter android:label="@string/app_name"&gt; &lt;action android:name="android.intent.action.SEND" /&gt; &lt;action android:name="android.intent.action.VIEW"/&gt; &lt;action android:name="android.intent.action.GET_CONTENT"/&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;!-- &lt;data android:mimeType="image/png" /&gt; --&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;uses-permission android:name="android.permission.INTERNET"&gt;&lt;/uses-permission&gt; &lt;/manifest&gt; </code></pre> <p>MyActivity.java</p> <pre><code>package com.example; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MyActivity extends Activity { private Button button; //String strURL="www.facebook.com"; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { final Context context=this; super.onCreate(savedInstanceState); setContentView(R.layout.main); button =(Button) findViewById(R.id.buttonurl); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(context,WebViewActivity.class); startActivity(intent); } }); } } </code></pre> <p>WebViewActivity.java</p> <pre><code>package com.example; import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; public class WebViewActivity extends Activity{ String url="http://www.stackoverflow.com"; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.webview); WebView webView= (WebView)findViewById(R.id.webView1); //MyWebViewClient myWebViewClient=new MyWebViewClient(); webView.setWebViewClient(new MyWebViewClient()); webView.loadUrl(url); // myWebViewClient.shouldOverrideUrlLoading(webView,url); } } </code></pre> <p>MyWebViewActivity.java</p> <pre><code>package com.example; import android.webkit.WebResourceResponse; import android.webkit.WebViewClient; import android.webkit.WebView; import android.net.Uri; import android.content.Intent; import android.app.Activity; import android.widget.Toast; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.HttpResponse; import org.apache.http.HttpEntity; import java.io.*; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; import org.apache.http.client.ClientProtocolException; public class MyWebViewClient extends WebViewClient { public InputStream httpcall(WebView view,String url) { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet("http://www.stackoverflow.com"); BufferedReader reader = null; String result = null; try { HttpResponse response = httpClient.execute(httpGet, localContext); result = ""; reader = new BufferedReader( new InputStreamReader( response.getEntity().getContent() ) ); String line = null; while ((line = reader.readLine()) != null) { result += line + "\n"; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( result.getBytes()); return byteArrayInputStream; //String webData = result.toString(); // return webData; //view.loadDataWithBaseURL("http://www.stackoverflow.com", webData, "text/html", "UTF-8", "about:blank"); } @Override public WebResourceResponse shouldInterceptRequest(WebView view,String url){ return new WebResourceResponse("text/json","utf-8",httpcall(view,url)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload