android实现联网小例子,刚学习与大家分享
2011-07-14 15:52
471 查看
首先是xml内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/address"
android:text="http://google.com"
/>
<Button android:id="@+id/buttongo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go!"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:textColor="#000000"
android:id="@+id/pagetext"
/>
</LinearLayout>
java类
package net.blog.vtion;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class GetWebPage extends Activity{
Handler h;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText etext=(EditText)findViewById(R.id.address);
final TextView tview=(TextView)findViewById(R.id.pagetext);
this.h=new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case 0:
tview.append((String)msg.obj);
break;
}
super.handleMessage(msg);
}
};
Button button=(Button)findViewById(R.id.buttongo);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
try{
tview.setText("");
URL url=new URL(etext.getText().toString());
URLConnection conn=url.openConnection();
BufferedReader rd=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line="";
while((line=rd.readLine())!=null){
Message lmsg;
lmsg=new Message();
lmsg.obj=line;
lmsg.what=0;
GetWebPage.this.h.sendMessage(lmsg);
}
}catch(Exception e){
}
}
});
}
}
最后一定要记得
manfest里写权限
<uses-permission android:name="android.permission.INTERNET"/>
里面用到什么直接查看api即可!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/address"
android:text="http://google.com"
/>
<Button android:id="@+id/buttongo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go!"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:textColor="#000000"
android:id="@+id/pagetext"
/>
</LinearLayout>
java类
package net.blog.vtion;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class GetWebPage extends Activity{
Handler h;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText etext=(EditText)findViewById(R.id.address);
final TextView tview=(TextView)findViewById(R.id.pagetext);
this.h=new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case 0:
tview.append((String)msg.obj);
break;
}
super.handleMessage(msg);
}
};
Button button=(Button)findViewById(R.id.buttongo);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
try{
tview.setText("");
URL url=new URL(etext.getText().toString());
URLConnection conn=url.openConnection();
BufferedReader rd=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line="";
while((line=rd.readLine())!=null){
Message lmsg;
lmsg=new Message();
lmsg.obj=line;
lmsg.what=0;
GetWebPage.this.h.sendMessage(lmsg);
}
}catch(Exception e){
}
}
});
}
}
最后一定要记得
manfest里写权限
<uses-permission android:name="android.permission.INTERNET"/>
里面用到什么直接查看api即可!
相关文章推荐
- 学习android笔记(7):实现微信和新浪微博分享(全)
- 一次偶然机遇找到一个不错的关于Android实现加载gif动画的实例,写一下分享个大家。
- 不使用第三方 SDK 实现简单分享 -- Android 学习之路
- Android学习 - 使用及实现系统分享接口
- 学习Android的一些网站收集 ,分享给大家
- Android学习之Android 5.0分享动画实现微信点击全屏效果
- Android中滑屏实现----手把手教你如何实现触摸滑屏以及Scroller类详解 (强烈推荐军哥的博客,大家多跟他学习学习)
- Android学习之Android 5.0分享动画实现微信点击全屏效果
- 刚开始学习android,做了个“二十一点”的小游戏,跟大家分享下!
- Android开发学习笔记-实现联网检测程序版本
- Android学习之微博SDK集成及实现分享到微博
- Android开发学习之分享功能的实现
- Android中实现Runnable接口简单例子
- 分享20个Android游戏源码,希望大家喜欢哈!
- 今天给大家分享一下Android中的资源与国际化的问题
- Android多媒体学习十一:实现仿百度图片查看功能
- Android 一个例子来解读Fragment实现横竖屏不同的布局
- Android开发学习之摇一摇功能的简单实现
- 给大家分享5个关于迅猛增长的实际例子