Android新手之旅(4) 通过HTTP访问web

1、访问web

manifest中应该增加一个

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

最好通过交互界面添加。代码:

public static String GetWebInfo2()
{
    String ret="";
    URL url=null;
    try
    {
        url=new URL("http://www.cnblogs.com/jetz/archive/2011/07/11/2102755.html");
     }
    catch(MalformedURLException e)
    {
        ret=e.getMessage();
        //Log.e("AccessWeb","MalformedURLException");
    }
    if(url!=null)
    {           
        try
        {
            HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
            InputStreamReader in=new InputStreamReader(urlConn.getInputStream());
            BufferedReader buffer=new BufferedReader(in);
            String inputLine=null;
            while(((inputLine=buffer.readLine())!=null))
            {
                ret+=inputLine+"\n";
            }
            in.close();
            urlConn.disconnect();               
        }
        catch(IOException e)
        {
            ret=e.getMessage();
        }
    }
    return ret;

}

posted @ 2011-07-17 10:07  jetz  阅读(462)  评论(0编辑  收藏  举报