webViewClientのshouldInterceptRequestメソッドを使用して、webViewでURLを開くAndroidアプリケーションを開発したいと思います。HttpClientを使用してデータを取得する必要があります。そこで、アプリケーションを作成しましたが、アプリケーションを実行すると、main.xmlが読み込まれ、ボタンをクリックすると、URLのhtmlコードが表示されます。
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));
}
}