您的位置:首页 > 理论基础 > 计算机网络

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、结束。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: