新大陆物联网Android开发实战(一)通过接入API文档获取新大陆物联网官网Logo-GETPOST多线程实战-网络资源下载

发布时间:2026/7/30 17:01:55
新大陆物联网Android开发实战(一)通过接入API文档获取新大陆物联网官网Logo-GETPOST多线程实战-网络资源下载 目录一、背景二、思路与实施权限配置布局设计布局代码接口类实现代码DownLoad类代码三、文章总结四、源码下载一、背景本文章是针对新大陆物联网竞赛其中Android开发的相关知识的总结与试验。本文主要通过Android程序与新大陆物联网端的通信使用多线程下载新大陆Logo。软件需求Android Studio二、思路与实施(本文章代码将在文章末尾免费提供)权限配置本文涉及到网络下载需要配置Android联网权限。uses-permission android:nameandroid.permission.INTERNET/布局设计布局代码?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:apphttp://schemas.android.com/apk/res-auto xmlns:toolshttp://schemas.android.com/tools android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationvertical tools:context.MainActivity ImageView android:idid/image android:layout_widthmatch_parent android:layout_height400dp android:srcmipmap/ic_launcher/ Button android:layout_widthmatch_parent android:layout_heightwrap_content android:onClickdown android:text下载/ /LinearLayout随后我们设计主程序主程序需要我们实现下载功能下载功能需要在子线程中实现下载后需要将资源传递至主线程因为我们需要写一个接口用于数据传输。接口类实现代码public interface Listener { void Data(byte[] data); }随后我们需要写一个下载类用于实现下载功能。DownLoad类代码首先我们初始化私有对象实现该类的构造方法在构造方法中传入下载地址。为了在主程序中能及时收到下载的图片我们需要在类中设置监听器。private Listener listener; public void setListener(Listener listener) { this.listenerlistener; }随后我们需要在主程序中初始化Imageview对象并设置好监听器。private DownLoad downLoad; private ImageView imageView; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView(ImageView) findViewById(R.id.image); } public void down(View view) { downLoadnew DownLoad(http://api.nlecloud.com/images/nlogo_blue_s.png); downLoad.setListener(new Listener() { Override public void Data(byte[] data) { imageView.setImageBitmap(BitmapFactory.decodeByteArray(data,0,data.length)); } }); downLoad.Down(); }可以看出上边的程序调用了类中的Public下载方法下面我们在类中实现这个方法。至此功能就实现啦。看效果图三、文章总结本文主要使用了HttpUrlConnection类URL类并结合了GETPOST以及多线程的知识这里暂时并没有用到异步任务文章比较简单可以说是必学知识点。四、源码下载新大陆物联网竞赛Android程序本题配套资源免积分下载