4. Android URL

 
添加   <uses-permission android:name="android.permission.INTERNET"/>
package com.east.zy;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpConnection;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
	private TextView text;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text=(TextView)findViewById(R.id.textView);
        
        connectWeb();
    }
    
    public String connectWeb()
    {
    	String str="";
    	try {
    		
			URL url=new URL("http://m.weather.com.cn/data/101180701.html");
			HttpURLConnection connect=(HttpURLConnection)url.openConnection();
//			connect.setRequestMethod("GET");
//			connect.setDoInput(true);
			InputStreamReader read=new InputStreamReader(connect.getInputStream());
			BufferedReader bf=new BufferedReader(read);
			
			String temp;
			while((temp=bf.readLine())!= null)
			{
				str += temp;
			}
			System.out.println(str);
			text.setText(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return str;
    }
}

你可能感兴趣的:(4. Android URL)