现如今开发的Android项目基本都需要进行到网络中进行资源浏览并下载。已经很少有仅靠单机操作的手机应用APP。本次文章我们主要介绍Android项目中如何进行连接网络操作,并通过一个现实网络图片的案例进行解释。
联网操作主要用到HttpURLConnection操作,注意事项,应为是联网耗时操作,需要开子线程。还有记得添加权限操作。
1、页面布局:(为方便起见我就只用线性布局了)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 。。。。。。。。。。。。。。。。。。。。。。。。。。。 android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入网址" android:id="@+id/wangzhi"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="dianji"