The case of downloading text and picture network programming based on HttpClient for Android
1, Operation effect
2, Implementation steps
1. Create android app DownloadTextImageByHttpClient
2, Main layout file 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. String resource file strings.xml
<resources> Download Text and pictures based on HttpClient < / String > < string name = "download_text" > Download Text < / String > < string name = "Download Image" > Download Image < / String > </resources>
4. Add support for HttpClient to build.gradle of the module
5. Main interface class 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);
// Using layout resource file to set up user interface
setContentView(R.layout.activity_main);
// Get control instance through resource ID
ivImage = (ImageView) findViewById(R.id.iv_image);
edtText = (EditText) findViewById(R.id.edt_text);
pbDownloadProgress = (ProgressBar) findViewById(R.id.pb_download_progress);
}
/**
* Download Image File
*
* @param view
*/
public void doDownloadImage(View view) {
// Get image file url string
String strImageUrl = SERVER_URL + "/test.jpg";
// Create Download Image task
DownloadImageTask task = new DownloadImageTask();
// Perform asynchronous tasks
task.execute(strImageUrl);
}
/**
* Download Image task (asynchronous task)
*/
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
/**
* What to do before carrying out the task
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
// Make progress bar visible
pbDownloadProgress.setVisibility(View.VISIBLE);
}
/**
* Work silently in the background
*
* @param strings
* @return
*/
@Override
protected Bitmap doInBackground(String... strings) {
// Get image URL string
String strImageUrl = strings[0];
// Define bitmap objects
Bitmap bitmap = null;
try {
// Create http request in GET mode
HttpGet request = new HttpGet(strImageUrl);
// Create http client
HttpClient client = new DefaultHttpClient();
// Execute http request, return response object
HttpResponse response = client.execute(request);
// Judge whether the response is successful (SC: State Code status code)
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// Get response data
HttpEntity entity = response.getEntity();
// Using bitmap factory to analyze response data and generate bitmap object
bitmap = BitmapFactory.decodeStream(entity.getContent());
} else {
Log.d(TAG, "Failed to download image file!");
}
} catch (IOException e) {
e.printStackTrace();
}
// Return bitmap object
return bitmap;
}
/**
* What to do after the task
*
* @param bitmap
*/
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
// Let the progress bar disappear
pbDownloadProgress.setVisibility(View.GONE);
// Display the bitmap object in the image control
ivImage.setImageBitmap(bitmap);
}
}
/**
* Download Text File
*
* @param view
*/
public void doDownloadText(View view) {
// Define text file URL string
String strTextUrl = SERVER_URL + "/test.txt";
// Create Download Text task
DownloadTextTask task = new DownloadTextTask();
// Perform asynchronous tasks
task.execute(strTextUrl);
}
/**
* Download Text task (asynchronous task)
*/
private class DownloadTextTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Make progress bar appear
pbDownloadProgress.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String... strings) {
// Get text file URL string
String strTextUrl = strings[0];
// Define text file content
String content = null;
try {
// Define http request for GET mode
HttpGet request = new HttpGet(strTextUrl);
// Define http client
HttpClient client = new DefaultHttpClient();
// Execute http request, get response object
HttpResponse response = client.execute(request);
// Judge whether the response is successful
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// Get response entity
HttpEntity entity = response.getEntity();
// Convert response entity data to string
content = EntityUtils.toString(entity, "UTF-8");
} else {
Log.d(TAG, "Failed to download text file!");
}
} catch (IOException e) {
e.printStackTrace();
}
// Return text file content
return content;
}
@Override
protected void onPostExecute(String content) {
super.onPostExecute(content);
// Let the progress bar disappear
pbDownloadProgress.setVisibility(View.GONE);
// Let content appear in the text box
edtText.setText(content);
}
}
}
6. Authorize access to the Internet in the project manifest file
7. Start the WebDemo project on the server side (IEDA new project)
8. Launch android app DownloadTextImageByHttpClient
9. End.