Android开发(13)-- 互联网访问图片,在android客户端显示

本文详细介绍了Android应用中布局界面的实现方法,并通过Java代码展示了如何进行URL的封装和网络请求,包括GET方式的请求及图片的获取。同时,提供了一个检查网络连接状态的封装类,以及输出流的封装类,帮助开发者更好地处理网络请求和数据传输。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、布局界面

  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity">

  10. <EditText
  11. android:id="@+id/url_text"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentLeft="true"
  15. android:layout_alignParentRight="true"
  16. android:layout_alignParentTop="true"
  17. android:ems="10"
  18. android:inputType="textPostalAddress"
  19. android:text="@string/url_text">

  20. <requestFocus />
  21. </EditText>

  22. <Button
  23. android:id="@+id/btn_text"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_alignLeft="@+id/url_text"
  27. android:layout_below="@+id/url_text"
  28. android:layout_marginTop="32dp"
  29. android:onClick="sendHttp"
  30. android:text="@string/btn_text"/>

  31. <ImageView
  32. android:id="@+id/iv_ie"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_alignParentBottom="true"
  36. android:layout_alignParentLeft="true"
  37. android:layout_alignRight="@+id/url_text"
  38. android:layout_below="@+id/btn_text"
  39. android:src="@drawable/ic_launcher"/>

  40. </RelativeLayout>
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <EditText  
  12.         android:id="@+id/url_text"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_alignParentRight="true"  
  17.         android:layout_alignParentTop="true"  
  18.         android:ems="10"  
  19.         android:inputType="textPostalAddress"  
  20.         android:text="@string/url_text" >  
  21.   
  22.         <requestFocus />  
  23.     </EditText>  
  24.   
  25.     <Button  
  26.         android:id="@+id/btn_text"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_alignLeft="@+id/url_text"  
  30.         android:layout_below="@+id/url_text"  
  31.         android:layout_marginTop="32dp"  
  32.         android:onClick="sendHttp"  
  33.         android:text="@string/btn_text" />  
  34.   
  35.     <ImageView  
  36.         android:id="@+id/iv_ie"  
  37.         android:layout_width="wrap_content"  
  38.         android:layout_height="wrap_content"  
  39.         android:layout_alignParentBottom="true"  
  40.         android:layout_alignParentLeft="true"  
  41.         android:layout_alignRight="@+id/url_text"  
  42.         android:layout_below="@+id/btn_text"  
  43.         android:src="@drawable/ic_launcher" />  
  44.   
  45. </RelativeLayout>  

2、封转的一些类

URL的封装:

  1. package com.example.lession08_code.utis;

  2. import java.io.InputStream;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;

  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;

  7. public class HttpUtils {

  8. public static String sendGet(String path){
  9. String content=null;
  10. try{
  11. //设置访问的url
  12. URL url=new URL(path);
  13. //打开请求
  14. HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
  15. //设置请求的信息
  16. httpURLConnection.setRequestMethod("GET");
  17. //设置请求是否超时
  18. httpURLConnection.setConnectTimeout(5000);
  19. //判断服务器是否响应成功
  20. if(httpURLConnection.getResponseCode()==200){
  21. //获取响应的输入流对象
  22. InputStream is=httpURLConnection.getInputStream();
  23. byte data[]=StreamTools.isTodata(is);
  24. //把转换成字符串
  25. content=new String(data);
  26. //内容编码方式
  27. if(content.contains("gb2312")){
  28. content=new String(data,"gb2312");
  29. }
  30. }
  31. //断开连接
  32. httpURLConnection.disconnect();
  33. }catch(Exception e){
  34. e.printStackTrace();
  35. }

  36. return content;
  37. }


  38. public static Bitmap sendGets(String path){
  39. Bitmap bitmap=null;
  40. try{
  41. //设置访问的url
  42. URL url=new URL(path);
  43. //打开请求
  44. HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
  45. //设置请求的信息
  46. httpURLConnection.setRequestMethod("GET");
  47. //设置请求是否超时
  48. httpURLConnection.setConnectTimeout(5000);
  49. //判断服务器是否响应成功
  50. if(httpURLConnection.getResponseCode()==200){
  51. //获取响应的输入流对象
  52. InputStream is=httpURLConnection.getInputStream();
  53. //直接把is的流转换成Bitmap对象
  54. bitmap=BitmapFactory.decodeStream(is);
  55. }
  56. //断开连接
  57. httpURLConnection.disconnect();
  58. }catch(Exception e){
  59. e.printStackTrace();
  60. }

  61. return bitmap;
  62. }
  63. }
  1. package com.example.lession08_code.utis;  
  2.   
  3. import java.io.InputStream;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.URL;  
  6.   
  7. import android.graphics.Bitmap;  
  8. import android.graphics.BitmapFactory;  
  9.   
  10. public class HttpUtils {  
  11.   
  12.     public static String sendGet(String path){  
  13.         String content=null;  
  14.         try{  
  15.             //设置访问的url  
  16.             URL url=new URL(path);  
  17.             //打开请求  
  18.             HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();  
  19.             //设置请求的信息  
  20.             httpURLConnection.setRequestMethod("GET");  
  21.             //设置请求是否超时  
  22.             httpURLConnection.setConnectTimeout(5000);  
  23.             //判断服务器是否响应成功  
  24.             if(httpURLConnection.getResponseCode()==200){  
  25.                 //获取响应的输入流对象  
  26.                 InputStream is=httpURLConnection.getInputStream();  
  27.                 byte data[]=StreamTools.isTodata(is);  
  28.                 //把转换成字符串  
  29.                 content=new String(data);  
  30.                 //内容编码方式  
  31.                 if(content.contains("gb2312")){  
  32.                     content=new String(data,"gb2312");  
  33.                 }  
  34.             }  
  35.             //断开连接  
  36.             httpURLConnection.disconnect();  
  37.         }catch(Exception e){  
  38.             e.printStackTrace();  
  39.         }  
  40.           
  41.         return content;  
  42.     }  
  43.       
  44.       
  45.     public static Bitmap sendGets(String path){  
  46.         Bitmap bitmap=null;  
  47.         try{  
  48.             //设置访问的url  
  49.             URL url=new URL(path);  
  50.             //打开请求  
  51.             HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();  
  52.             //设置请求的信息  
  53.             httpURLConnection.setRequestMethod("GET");  
  54.             //设置请求是否超时  
  55.             httpURLConnection.setConnectTimeout(5000);  
  56.             //判断服务器是否响应成功  
  57.             if(httpURLConnection.getResponseCode()==200){  
  58.                 //获取响应的输入流对象  
  59.                 InputStream is=httpURLConnection.getInputStream();  
  60.                 //直接把is的流转换成Bitmap对象  
  61.                 bitmap=BitmapFactory.decodeStream(is);  
  62.             }  
  63.             //断开连接  
  64.             httpURLConnection.disconnect();  
  65.         }catch(Exception e){  
  66.             e.printStackTrace();  
  67.         }  
  68.           
  69.         return bitmap;  
  70.     }  
  71. }  


判断网络是否连接的封装类

  1. package com.example.lession08_code.utis;

  2. import android.app.AlertDialog;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.net.ConnectivityManager;
  8. import android.net.NetworkInfo;
  9. import android.widget.Toast;

  10. public class NetWorkUtils {

  11. private Context context;

  12. // 网路链接管理对象
  13. public ConnectivityManager connectivityManager;

  14. public NetWorkUtils(Context context) {
  15. this.context = context;
  16. // 获取网络链接的对象
  17. connectivityManager = (ConnectivityManager) context
  18. .getSystemService(Context.CONNECTIVITY_SERVICE);
  19. }

  20. public boolean setActiveNetWork() {
  21. boolean flag=false;
  22. // 获取可用的网络链接对象
  23. NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  24. if (networkInfo == null) {
  25. new AlertDialog.Builder(context)
  26. .setTitle("网络不可用")
  27. .setMessage("可以设置网络?")
  28. .setPositiveButton("确认",
  29. new DialogInterface.OnClickListener() {
  30. @Override
  31. public void onClick(DialogInterface dialog,
  32. int which) {
  33. Toast.makeText(context, "点击确认",
  34. Toast.LENGTH_LONG).show();


  35. // 声明意图
  36. Intent intent = new Intent();
  37. intent.setAction(Intent.ACTION_MAIN);
  38. intent.addCategory("android.intent.category.LAUNCHER");
  39. intent.setComponent(new ComponentName(
  40. "com.android.settings",
  41. "com.android.settings.Settings"));
  42. intent.setFlags(0x10200000);
  43. // 执行意图
  44. context.startActivity(intent);

  45. }

  46. })
  47. .setNegativeButton("取消",
  48. new DialogInterface.OnClickListener() {

  49. @Override
  50. public void onClick(DialogInterface dialog,
  51. int which) {
  52. }

  53. }).show();// 必须.show();

  54. }

  55. if(networkInfo!=null){
  56. flag=true;
  57. }

  58. return flag;
  59. }
  60. }
  1. package com.example.lession08_code.utis;  
  2.   
  3. import android.app.AlertDialog;  
  4. import android.content.ComponentName;  
  5. import android.content.Context;  
  6. import android.content.DialogInterface;  
  7. import android.content.Intent;  
  8. import android.net.ConnectivityManager;  
  9. import android.net.NetworkInfo;  
  10. import android.widget.Toast;  
  11.   
  12. public class NetWorkUtils {  
  13.   
  14.     private Context context;  
  15.   
  16.     // 网路链接管理对象  
  17.     public ConnectivityManager connectivityManager;  
  18.   
  19.     public NetWorkUtils(Context context) {  
  20.         this.context = context;  
  21.         // 获取网络链接的对象  
  22.         connectivityManager = (ConnectivityManager) context  
  23.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
  24.     }  
  25.   
  26.     public boolean setActiveNetWork() {  
  27.         boolean flag=false;  
  28.         // 获取可用的网络链接对象  
  29.         NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();  
  30.         if (networkInfo == null) {  
  31.             new AlertDialog.Builder(context)  
  32.                     .setTitle("网络不可用")  
  33.                     .setMessage("可以设置网络?")  
  34.                     .setPositiveButton("确认",  
  35.                             new DialogInterface.OnClickListener() {  
  36.                                 @Override  
  37.                                 public void onClick(DialogInterface dialog,  
  38.                                         int which) {  
  39.                                     Toast.makeText(context, "点击确认",  
  40.                                             Toast.LENGTH_LONG).show();  
  41.   
  42.                                       
  43.                                     // 声明意图  
  44.                                     Intent intent = new Intent();  
  45.                                     intent.setAction(Intent.ACTION_MAIN);  
  46.                                     intent.addCategory("android.intent.category.LAUNCHER");  
  47.                                     intent.setComponent(new ComponentName(  
  48.                                             "com.android.settings",  
  49.                                             "com.android.settings.Settings"));  
  50.                                     intent.setFlags(0x10200000);  
  51.                                     // 执行意图  
  52.                                     context.startActivity(intent);  
  53.   
  54.                                 }  
  55.   
  56.                             })  
  57.                     .setNegativeButton("取消",  
  58.                             new DialogInterface.OnClickListener() {  
  59.   
  60.                                 @Override  
  61.                                 public void onClick(DialogInterface dialog,  
  62.                                         int which) {  
  63.                                 }  
  64.   
  65.                             }).show();// 必须.show();  
  66.   
  67.         }  
  68.           
  69.         if(networkInfo!=null){  
  70.             flag=true;  
  71.         }  
  72.           
  73.         return flag;  
  74.     }  
  75. }  


输出流的封装类

  1. package com.example.lession08_code.utis;

  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;

  5. public class StreamTools {

  6. public staticbyte[] isTodata(InputStream is) throws IOException{
  7. //字节输出流
  8. ByteArrayOutputStream bops=new ByteArrayOutputStream();
  9. //读取数据的缓冲区
  10. byte buffer[]=newbyte[1024];
  11. //读取记录的长度
  12. int len=0;
  13. while((len=is.read(buffer))!=-1){
  14. bops.write(buffer, 0, len);
  15. }
  16. //把读取的内容转换成byte数组
  17. byte data[]=bops.toByteArray();
  18. return data;
  19. }
  20. }
  1. package com.example.lession08_code.utis;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6.   
  7. public class StreamTools {  
  8.   
  9.     public static byte[] isTodata(InputStream is) throws IOException{  
  10.         //字节输出流  
  11.         ByteArrayOutputStream bops=new ByteArrayOutputStream();  
  12.         //读取数据的缓冲区  
  13.         byte buffer[]=new byte[1024];  
  14.         //读取记录的长度  
  15.         int len=0;  
  16.         while((len=is.read(buffer))!=-1){  
  17.             bops.write(buffer, 0, len);  
  18.         }  
  19.         //把读取的内容转换成byte数组  
  20.         byte data[]=bops.toByteArray();  
  21.         return data;  
  22.     }  
  23. }  



注意:在这里还需要加权限问题

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值