Android之基于HttpClient下载文本与图片下载文本与图片网络编程案例
2017-12-30 16:17
483 查看
Android之基于HttpClient下载文本与图片网络编程案例
一、运行效果二、实现步骤
1、创建安卓应用DownloadTextImageByHttpClient
2、主布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="15dp" tools:context="net.lm.ied.download_text_image_by_httpclient.MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:gravity="center" android:layout_marginBottom="20px" android:layout_height="wrap_content"> <Button android:id="@+id/btn_download_text" android:layout_width="150dp" android:layout_height="wrap_content" android:onClick="doDownloadText" android:text="@string/download_text"/> <Button android:id="@+id/btn_download_image" android:layout_width="150dp" android:layout_height="wrap_content" android:onClick="doDownloadImage" android:text="@string/download_image"/> </LinearLayout> <ProgressBar android:id="@+id/pb_download_progress" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone"/> <ImageView android:id="@+id/iv_image" android:layout_width="match_parent" android:layout_height="250dp" android:scaleType="fitXY"/> <EditText android:id="@+id/edt_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:lines="8" android:editable="false" android:scrollbars="vertical"/> </LinearLayout>
3、字符串资源文件strings.xml
<resources> <string name="app_name">基于HttpClient下载文本与图片</string> <string name="download_text">下载文本</string> <string name="download_image">下载图片</string> </resources>
4、在模块的build.gradle里添加对HttpClient的支持
5、主界面类MainActivity
package net.lm.ied.download_text_image_by_httpclient; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.ProgressBar; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import java.io.IOException; public class MainActivity extends Activity { private final String TAG = "download_text_image"; private final String SERVER_URL = "http://192.168.183.2:8080/lzy_server"; private ImageView ivImage; private EditText edtText; private Bitmap bitmap; private String text; private ProgressBar pbDownloadProgress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 利用布局资源文件设置用户界面 setContentView(R.layout.activity_main); // 通过资源标识获取控件实例 ivImage = (ImageView) findViewById(R.id.iv_image); edtText = (EditText) findViewById(R.id.edt_text); pbDownloadProgress = (ProgressBar) findViewById(R.id.pb_download_progress); } /** * 下载图像文件 * * @param view */ public void doDownloadImage(View view) { // 获取图像文件url字符串 String strImageUrl = SERVER_URL + "/test.jpg"; // 创建下载图像任务 DownloadImageTask task = new DownloadImageTask(); // 执行异步任务 task.execute(strImageUrl); } /** * 下载图像任务(异步任务) */ private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { /** * 执行任务之前要做的事情 */ @Override protected void onPreExecute() { super.onPreExecute(); // 让进度条可见 pbDownloadProgress.setVisibility(View.VISIBLE); } /** * 在后台默默工作 * * @param strings * @return */ @Override protected Bitmap doInBackground(String... strings) { // 获取图像网址字符串 String strImageUrl = strings[0]; // 定义位图对象 Bitmap bitmap = null; try { // 创建GET方式的http请求 HttpGet request = new HttpGet(strImageUrl); // 创建http客户端 HttpClient client = new DefaultHttpClient(); // 执行http请求,返回响应对象 HttpResponse response = client.execute(request); // 判断是否响应成功(SC: State Code 状态码) if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 获取响应数据 HttpEntity entity = response.getEntity(); // 利用位图工厂解析响应数据,生成位图对象 bitmap = BitmapFactory.decodeStream(entity.getContent()); } else { Log.d(TAG, "下载图像文件失败!"); } } catch (IOException e) { e.printStackTrace(); } // 返回位图对象 return bitmap; } /** * 执行任务之后要做的事情 * * @param bitmap */ @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); // 让进度条消失 pbDownloadProgress.setVisibility(View.GONE); // 将bitmap对象显示在图像控件里 ivImage.setImageBitmap(bitmap); } } /** * 下载文本文件 * * @param view */ public void doDownloadText(View view) { // 定义文本文件网址字符串 String strTextUrl = SERVER_URL + "/test.txt"; // 创建下载文本任务 DownloadTextTask task = new DownloadTextTask(); // 执行异步任务 task.execute(strTextUrl); } /** * 下载文本任务(异步任务) */ private class DownloadTextTask extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { super.onPreExecute(); // 让进度条出现 pbDownloadProgress.setVisibility(View.VISIBLE); } @Override protected String doInBackground(String... strings) { // 获取文本文件网址字符串 String strTextUrl = strings[0]; // 定义文本文件内容 String content = null; try { // 定义GET方式的http请求 HttpGet request = new HttpGet(strTextUrl); // 定义http客户端 HttpClient client = new DefaultHttpClient(); // 执行http请求,获取响应对象 HttpResponse response = client.execute(request); // 判断是否响应成功 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 获取响应实体 HttpEntity entity = response.getEntity(); // 将响应实体数据转换成字符串 content = EntityUtils.toString(entity, "UTF-8"); } else { Log.d(TAG, "下载文本文件失败!"); } } catch (IOException e) { e.printStackTrace(); } // 返回文本文件内容 return content; } @Override protected void onPostExecute(String content) { super.onPostExecute(content); // 让进度条消失 pbDownloadProgress.setVisibility(View.GONE); // 让content显示在文本框里 edtText.setText(content); } } }
6、在项目清单文件里授权访问因特网
7、启动服务器端的WebDemo项目(IEDA新建项目)
8、启动安卓应用DownloadTextImageByHttpClient
9、结束。
相关文章推荐
- 安卓案例:基于HttpClient下载文本与图片
- 安卓案例:基于HttpURLConnection下载文本与图片
- Android开发之网络请求通信专题(二):基于HttpClient的文件上传下载
- Android基于TCP和URL协议的网络编程示例【附demo源码下载】
- Java基础知识强化之网络编程笔记18:Android网络通信之 使用HttpClient的Post / Get 方式读取网络数据(基于HTTP通信技术)
- Android学习篇章45-HttpClient网络下载-IO流传输-下载图片资源
- android网络编程--从网络下载图片,并保存到内存卡
- android网络编程--从网络下载图片,并保存到内存卡
- Android开发之异步获取并下载网络资源-下载图片和下载文本内容
- Android 下载网络图片注意的问题
- Android异步下载网络图片(其一)
- android 通过http网络下载图片 并
- android下载网络图片并缓存
- android下载网络图片并显示
- Android异步从网络下载图片并且缓存图片到本地的demo
- Android异步下载网络图片(其一)
- 完美解决 android 网络下载图片 outofmemery 问题!
- Android异步下载网络图片(其一:Handler)
- Android网络编程实践之旅(二):基于Socket的网络通信
- 异步网络编程之图片批量抓取下载(C#)