Skip to content

Instantly share code, notes, and snippets.

@hisasann
Created November 8, 2012 09:40
Show Gist options
  • Save hisasann/4037805 to your computer and use it in GitHub Desktop.
Save hisasann/4037805 to your computer and use it in GitHub Desktop.
AndroidでWebViewのCookieを共有する方法
package com.hisasann.sample_cookieshare;
import java.io.IOException;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.CookieManager;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
@SuppressLint({ "SetJavaScriptEnabled" })
public class MainActivity extends Activity {
private DefaultHttpClient httpClient = new DefaultHttpClient();
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDefaultTextEncodingName("UTF-8");
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
System.out.println("onPageFinished - " + url);
super.onPageFinished(view, url);
// Cookieの保存が予想されるURLの場合
if (url.indexOf("http://lab.hisasann.com/") > -1) {
// WebViewのCookieを取得
String cookie = CookieManager.getInstance().getCookie(url);
System.out.println("cookie - " + cookie);
String[] cookies = cookie.split(";");
for (String keyValue : cookies) {
keyValue = keyValue.trim();
String[] cookieSet = keyValue.split("=");
// Cookieを作成
BasicClientCookie bCookie = new BasicClientCookie(cookieSet[0], cookieSet[1]);
bCookie.setDomain("lab.hisasann.com");
bCookie.setPath("/");
// CookieStoreを取得
CookieStore store = httpClient.getCookieStore();
// Cookieを追加
store.addCookie(bCookie);
}
}
}
});
webview.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return false;
}
@Override
public void onProgressChanged(WebView view, int progress) {
super.onProgressChanged(view, progress);
}
});
webview.loadUrl("http://lab.hisasann.com/cookie/setcookie.php");
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CookieStore store = httpClient.getCookieStore();
List<Cookie> cookies = store.getCookies();
for (Cookie cookie : cookies) {
// クッキーの設定
if ("lab.hisasann.com".equals(cookie.getDomain())) {
// クッキーを再設定
System.out.println(cookie.getName() + "=" + cookie.getValue());
break;
}
}
httpGet();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void httpGet() {
HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10 * 1000);
HttpGet httpGet = new HttpGet("http://lab.hisasann.com/cookie/getcookie.php");
try {
httpClient.execute(httpGet, new ResponseHandler<String>() {
@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
switch (response.getStatusLine().getStatusCode()) {
case HttpStatus.SC_OK:
System.out.println(EntityUtils.toString(response.getEntity(), "UTF-8"));
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
System.out.println(cookies);
}
return null;
}
});
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment