佈局文件php
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="match_parent" android:layout_height="50dip" > <Button android:layout_alignParentLeft="true" android:text="返回" android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="40dip" /> <TextView android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="40dip" android:id="@+id/tv" /> <Button android:layout_alignParentRight="true" android:text="刷新" android:id="@+id/refresh" android:layout_width="wrap_content" android:layout_height="40dip" /> </RelativeLayout> <WebView android:id="@+id/wv" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
MainActivityjava
package com.tang.web_01; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.TextureView; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.webkit.CookieManager; import android.webkit.CookieSyncManager; import android.webkit.DownloadListener; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.TextView; import android.os.Build; import org.apache.http.cookie.Cookie; public class MainActivity extends Activity implements OnClickListener { private WebView wv; private TextView tv; private Button back; private Button refresh; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { //設置cookie; CookieSyncManager.createInstance(MainActivity.this); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true); Cookie cookie = (Cookie) msg.obj; String str = cookie.getName()+"="+cookie.getValue(); cookieManager.setCookie("192.168.0.105", str); CookieSyncManager.getInstance().sync(); wv.loadUrl("http://192.168.0.105/index.php"); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wv = (WebView) findViewById(R.id.wv); back = (Button) findViewById(R.id.back); refresh = (Button) findViewById(R.id.refresh); tv = (TextView) findViewById(R.id.tv); back.setOnClickListener(this); refresh.setOnClickListener(this); wv.setDownloadListener(new MyDownloadListener()); //wv.loadUrl("http://192.168.0.105/index.php"); /* wv.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub view.loadUrl(url); return super.shouldOverrideUrlLoading(view, url); } }); wv.setWebChromeClient(new WebChromeClient(){ @Override public void onReceivedTitle(WebView view, String title) { tv.setText(title); super.onReceivedTitle(view, title); } });*/ new HttpCookie(handler).start(); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.back: finish(); break; case R.id.refresh: wv.reload(); break; } } private class MyDownloadListener implements DownloadListener { public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { System.out.println(url); if(url.endsWith(".zip")){ new HttpThread(url).start(); //Uri uri = Uri.parse(url); //Intent intent = new Intent(Intent.ACTION_VIEW, uri); //startActivity(intent); } } } }
HttpThreadandroid
package com.tang.web_01; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.apache.http.client.HttpClient; import android.os.Environment; public class HttpThread extends Thread { private String murl; public HttpThread(String url) { murl = url; } public void run() { try { URL httpurl = new URL(murl); HttpURLConnection conn = (HttpURLConnection) httpurl.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); InputStream in = conn.getInputStream(); FileOutputStream out = null; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File file = new File(Environment.getExternalStorageDirectory(), "test2.apk"); out = new FileOutputStream(file); } byte[] b = new byte[6*1024]; int len; while((len = in.read(b)) != -1){ if(out != null){ out.write(b, 0, len); } } System.out.print("download success"); } catch (Exception e) { // TODO Auto-generated catch block System.out.print("exceptioin"); e.printStackTrace(); } } }
HtttpCookieweb
package com.tang.web_01; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.AbstractHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import android.os.Handler; import android.os.Message; public class HttpCookie extends Thread{ private Handler handler; public HttpCookie(Handler hand) { handler = hand; } public void run() { //從服務器獲取cookie String path = "http://192.168.0.105/test.php"; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(path); List<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("user", "user")); parameters.add(new BasicNameValuePair("pass", "pass")); try { post.setEntity(new UrlEncodedFormEntity(parameters)); HttpResponse response = client.execute(post); int code = response.getStatusLine().getStatusCode(); if (code == 200) { AbstractHttpClient absClient = (AbstractHttpClient) client; List<Cookie> cookies = absClient.getCookieStore().getCookies(); for (Cookie cookie:cookies) { Message msg = new Message(); System.out.println(cookie); msg.obj = cookie; handler.sendMessage(msg); } } } catch (Exception e) { e.printStackTrace(); } } }