-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
204 lines (159 loc) · 6.78 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.example.shannong_day8_httpclient;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final String TAG = "HttpClient";
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.image_view);
file2Bitmap();
}
/**
* 使用HttpClient来进行GET请求
*/
private void httpClientGet() {
new Thread() {
@Override
public void run() {
// 基础参数对象
BasicHttpParams params = new BasicHttpParams();
// 使用静态方法
// 设置连接时长
// 第一个参数:BasicHttpParams对象
// 第二个参数:设置时间
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
// 获得HttpClient对象
HttpClient client = new DefaultHttpClient(params);
// 实例化Get请求对象
HttpGet get = new HttpGet(
"http://v.juhe.cn/toutiao/index?type=tiyu&key=5465c4c5d60f72c3d756a9f1a9b8437d");
try {
// 执行get请求,获得返回的数据封装
HttpResponse response = client.execute(get);
// 判断状态码
if (response.getStatusLine().getStatusCode() == 200) {
// 获取字节输入流
InputStream is = response.getEntity().getContent();
// 转换为字符流输入流
InputStreamReader isr = new InputStreamReader(is);
// 缓冲字符输入流
BufferedReader br = new BufferedReader(isr);
// JSON数据拼接用的字符串类
StringBuffer sb = new StringBuffer();
// “水瓢”
String buffer = new String();
// 循环读取数据
while ((buffer = br.readLine()) != null) {
// 追加拼接的字符串
sb.append(buffer);
}
// 转换成字符串,打印检测结果是否正常
Log.d(TAG, sb.toString());
// 关闭输入流
br.close();
} else {
Log.d(TAG, "Error code:" + response.getStatusLine().getStatusCode());
}
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "" + e);
}
}
}.start();
}
/**
* 从网络获取图片的输入流,并且将输入流转换成Bitmap用于UI显示
*/
private void getInputStreamFromNet() {
new Thread() {
public void run() {
// 基础参数对象
BasicHttpParams params = new BasicHttpParams();
// 使用静态方法
// 设置连接时长
// 第一个参数:BasicHttpParams对象
// 第二个参数:设置时间
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
// 获得HttpClient对象
HttpClient client = new DefaultHttpClient(params);
HttpGet get = new HttpGet(
"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1530532248868&di=0fccebc2c4911e0520b9b84db949b0b3&imgtype=0&src=http%3A%2F%2Fwww.sdtajg.com%2FimageRepository%2F401a6292-6cf9-4d0f-850a-29fbb08c5271.jpg");
try {
// 执行get请求,获得返回的数据封装
HttpResponse response = client.execute(get);
// 判断状态码
if (response.getStatusLine().getStatusCode() == 200) {
// 获取字节输入流
InputStream is = response.getEntity().getContent();
// TODO 转Bitmap 或者 文件存储
final Bitmap bitmap = BitmapFactory.decodeStream(is);
// 显示在控件(主线程)
runOnUiThread(new Runnable() {
@Override
public void run() {
// 主线程要执行的代码
mImageView.setImageBitmap(bitmap);
}
});
// 准备一个输出流用于输出Bitmap到文件
FileOutputStream fos = new FileOutputStream(getCacheDir() + File.separator + "demo.jpg");
// 套缓冲流
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 将Bitmap缓存成文件
// 第一个参数:要压缩的格式
// 第二个参数:压缩品质,0是最小文件,100是最高品质
//
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
is.close();
} else {
Log.d(TAG, "Error code:" + response.getStatusLine().getStatusCode());
}
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "" + e);
}
}
}.start();
}
/**
* 从缓存中读取图片为Bitmap,UI显示
*/
private void file2Bitmap() {
new Thread() {
public void run() {
// 从文件读取到Bitmap,参数为文件路径
final Bitmap bitmap = BitmapFactory.decodeFile(getCacheDir() + File.separator + "demo.jpg");
// 通知主线程显示
runOnUiThread(new Runnable() {
public void run() {
mImageView.setImageBitmap(bitmap);
}
});
}
}.start();
}
}