zoukankan      html  css  js  c++  java
  • 安卓开发--HttpDemo01

    package com.cnn.httpdemo01;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    
    public class MainActivity extends Activity {
    	Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            button = (Button) findViewById(R.id.button1);
            button.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO 自动生成的方法存根
    				Intent intent = new Intent(MainActivity.this, BaiDUActivity.class);
    				startActivity(intent);
    			}
    		});
        }
    
    }
    

      

    package com.cnn.httpdemo01;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import android.os.Handler;
    import android.webkit.WebView;
    
    public class HttpThread extends Thread {
    	private WebView webView;
    	private String strURL;
    	private Handler handler;
    	
    	public HttpThread(WebView webView,String strURL,Handler handler){
    		this.webView=webView;
    		this.strURL=strURL;
    		this.handler=handler;
    	}
    	
    	@Override
    	public void run() {
    		URL url;
    		try {
    			url=new URL(strURL);
    			HttpURLConnection connection=(HttpURLConnection) url.openConnection();
    			connection.setReadTimeout(5000);
    			connection.setRequestMethod("GET");
    			final StringBuffer stringBuffer = new StringBuffer();
    			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    			String str;
    			while ((str=bufferedReader.readLine())!=null) {
    				stringBuffer.append(str);				
    			}
    			handler.post(new Runnable() {
    				
    				@Override
    				public void run() {
    					// TODO 自动生成的方法存根
    					webView.loadData(stringBuffer.toString(), "text/html;charset=utf-8", null);
    				}
    			});
    			
    		} catch (MalformedURLException e) {
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		} catch (IOException e) {			
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		}
    		
    	}
    }
    

      

    package com.cnn.httpdemo01;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.webkit.WebView;
    
    public class BaiDUActivity extends Activity {
    	private WebView webView;
    	private String strURL;
    	private Handler handler = new Handler();
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO 自动生成的方法存根
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.httpbaidu);
    		
    		webView=(WebView) findViewById(R.id.webView1);
    		strURL = "http://www.baidu.com/";
    		new HttpThread(webView, strURL, handler).start();
    	}
    	
    
    }
    

      

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.cnn.httpdemo01.MainActivity" >
    
        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="百度" />
    
    </RelativeLayout>
    

      

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <WebView
            android:id="@+id/webView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    

      

  • 相关阅读:
    eclipse rcp 获取工程项目路径
    Eclipse RCP中添加第三方jar包的办法
    eclipse content assist 代码提示功能失效解决办法
    lwuit更改字体大小
    lwuit调整滚动条灵敏度值
    AWTEvent
    IE7 IE6去掉关闭提示框的解决方案
    jQuery多库共存最优解决方案
    电子商务网站 数据库产品表设计方案
    javascript操作cookie
  • 原文地址:https://www.cnblogs.com/zxcnn/p/5080703.html
Copyright © 2011-2022 走看看