Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8507913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:00:19+00:00 2026-06-11T03:00:19+00:00

I want to develop an android application that would open a url in a

  • 0

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.

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >
<Button
        android:id="@+id/buttonurl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="visit     www.stackoverflow.com"
        />


</LinearLayout>

webview.xml

<?xml version="1.0" encoding="utf-8"?>

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/webView1"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"/>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example"
      android:versionCode="1"
      android:versionName="1.0">
<uses-sdk android:minSdkVersion="16"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
    <activity android:name="MyActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".WebViewActivity" android:label="@string/app_name">
        <intent-filter android:label="@string/app_name">
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.VIEW"/>
            <action android:name="android.intent.action.GET_CONTENT"/>
            <category android:name="android.intent.category.DEFAULT" />
            <!--    <data android:mimeType="image/png" /> -->
        </intent-filter>


    </activity>
</application>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

MyActivity.java

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);
        }
    });



}
}

WebViewActivity.java

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);



}
}

MyWebViewActivity.java

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));
   }







}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:00:21+00:00Added an answer on June 11, 2026 at 3:00 am

    Please Add this setContentView(R.layout.webview); line into your WebViewActivity class before WebView webView= (WebView)findViewById(R.id.webView1); and write instead of , it will solve your problem.

    and change below line of your activity.

    String url="http://www.stackoverflow.com"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've to develop an android application that allows users to chat I want to
I want to develop an application for blind using J2ME or Android. but i
I new in the android developing. I want to develop simple application that will
I want to develop an Android application that visualize Dicom files and present them
I want to develop application that sends image/txt or any file from one android
I want to develop an android English dictionary application that uses only an offline
I am using Eclipse to develop an Android application that plots Bluetooth data. I
i want to develop an application on android,webos,wince. how to develop application that should
I want to develop an android application, that displays live cricket scores as well
I want to develop an android application, that gets my current location thru GPS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.