学了快一个月的安卓了,在网上学了一个小小的案例:如下
在Tomcat服务器上保存几张图片,在写个Android应用,要求能够点击上一张和下一张切换图片(图片必须是从服务器上获取的)
刚拿到这一个题目的时候,感觉挺难的,但是慢慢的做下去,感觉也不是很难,贵在能够坚持下去把!
第一步:在Tomcat服务器上配置相应的photo.txt文件和相应的images文件夹
文件夹中的文件:
这样就在Tomcat中配置好了所要的文件。
第二步:建立Android项目,编码
①:在第一个界面中建立一个按钮组件,实现到第二个页面的跳转,在第二个界面中添加ImageView和两个上一张和下一张的按钮
这是两个界面的效果图
具体的布局代码是:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.yzg.photolooker.MainActivity"> /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="跳转到图片查看器" android:textSize="30dp" android:textStyle="bold" android:onClick="photoLook" /> </android.support.v7.widget.LinearLayoutCompat> <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.yzg.photolooker.Main2Activity"> <ImageView android:id="@+id/image_iv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="上一张" android:onClick="preClick" /> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="下一张" android:onClick="backClick" /> </LinearLayout> </android.support.v7.widget.LinearLayoutCompat>这样基本的界面布局就做好了
②第一个界面的逻辑代码也是很简单的,点击按钮到第二个界面的跳转即可
package com.example.yzg.photolooker; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void photoLook(View v){ Intent intent = new Intent(MainActivity.this, Main2Activity.class); startActivity(intent); } }③第二个界面的逻辑代码,实现从Tomcat中获取图片,显示在第二个界面上
在这个界面的逻辑代码中一共分为三部分:
第一:从photo.txt中获取每个图片的URL,封装成一个列表
第二:将列表中的第一个图片显示在手机界面上
第三:实现上一张和下一张功能
下面给出的代码只实现前两步,至于后两步自己还在研究中。
package com.example.yzg.photolooker; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; public class Main2Activity extends AppCompatActivity { private String temp; List imagesUrlpaths = new ArrayList<String>(); ImageView mimage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); mimage = findViewById(R.id.image_iv); //需求: //加载服务器上的图片 然后点击上一站或者下一张时 图片会自动变换 //1。配置Tomcat服务器 将图片添加到服务器上 photo.txt: http://127.0.0.1:8080/androiddemo/photo.txt //2。从网络中获取photo.txt文件 将其中的图片的URL地址封装成ArrayList new Thread(){ public void run(){ try { //获取一个URL对象 //易错点 URL和URI千万不能搞错了 URL uri = new URL(getPhotoUrlPath()); //获取一个连接 HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); //获取状态码 int responseCode = conn.getResponseCode(); //如果状态码是200 则访问成功 把文件里的信息存在ArrayList并打印出来 if (responseCode == 200){ InputStream inputStream = conn.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); while ((temp = bufferedReader.readLine())!=null){ imagesUrlpaths.add(temp); } } //打印列表中的信息 for (int i = 0; i < imagesUrlpaths.size(); i++) { Log.d("测试", "onCreate: "+imagesUrlpaths.get(i)); } URL urlimagepath = new URL((String) imagesUrlpaths.get(0)); HttpURLConnection connection = (HttpURLConnection) urlimagepath.openConnection(); int responseCode1 = connection.getResponseCode(); if (responseCode1 == 200){ InputStream inputStream = connection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); mimage.setImageBitmap(bitmap); } } catch (Exception e) { e.printStackTrace(); } } }.start(); //3.定义一个索引 对应第二步的列表 最终实现点击上一张实现跳转 } // 原来模拟器默认把127.0.0.1和localhost当做本身了 // 在模拟器上可以用10.0.2.2代替127.0.0.1和localhost // 另外如果是在局域网环境可以用 192.168.0.x或者192.168.1.x(根据具体配置) // 连接本机,这样应该就不会报错了 private String getPhotoUrlPath(){ return "http://10.0.2.2:8080/androiddemo/photo.txt"; } //这是点击上一张图片的逻辑代码 public void preClick(View view) { } //这是点击下一张图片的逻辑代码 public void backClick(View view) { } }经验之谈:
1.忘记给自己的应用添加一些应用权限,这次忘记添加了使用网络的权限。
2. 从浏览器中访问Tomca服务器是127.0.0.1,而从手机模拟器中访问是10.0.2.2,这是出现第二个BUG的解决办法。
3.不能再主线程中访问网络,出现了android.os.NetworkOnMainThreadException这个错误,具体的问题请看下面这张图:
因为自己对于在Android使用线程,自己还不太熟悉,所以打算在下一篇博客中来说一说,并实现上一张个下一张按钮的功能。
最后,有兴趣的小伙伴可以一起交流啊!