参考:http://blog.csdn.net/zpj779878443/article/category/6351593
- 建立界面,一个imageView,两个button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bbw.gallery.MainActivity">
<ImageView
android:id="@+id/image_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/image_show">
<Button
android:id="@+id/change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="换一批" />
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一个" />
</LinearLayout>
</RelativeLayout>
- 加载图片
package com.example.bbw.gallery.loader;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by bbw on 2017/7/17.
* 加载图片
*/
public class PictureLoader {
private ImageView imageView;
private String imgUrl;
private byte[] picByte;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x123){
if (picByte != null){
Bitmap bitmap = BitmapFactory.decodeByteArray(picByte,0,picByte.length);//将字节数组解码成bitmap
imageView.setImageBitmap(bitmap);
}
}
}
};
public void load(ImageView imageView,String imgUrl){
this.imageView = imageView;
this.imgUrl = imgUrl;
Drawable drawable = imageView.getDrawable();
if (drawable != null && drawable instanceof BitmapDrawable){
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
if (bitmap != null && !bitmap.isRecycled()){
bitmap.recycle();//回收图片所占的内存
}
}
new Thread(runnable).start();
}
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
URL url = new URL(imgUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(8000);
if (connection.getResponseCode() == 200){
InputStream in = connection.getInputStream();//获得输入流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int length = -1;
while((length = in.read(bytes)) !=-1){
out.write(bytes,0,length);//以字节的方式写出
}
picByte = out.toByteArray();
in.close();
out.close();
handler.sendEmptyMessage(0x123);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
- 网络请求处理相关的类
package com.example.bbw.gallery.network;
import android.util.Log;
import com.example.bbw.gallery.entity.Sister;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
/**
* Created by bbw on 2017/7/17.
* 网络请求处理相关类
*/
public class Api {
private static final String TAG = "Network";
private static final String BASE_URL = "http://gank.io/api/data/福利/";
//查询信息
public ArrayList<Sister> fetchSister(int count, int page){
String fetchUrl = BASE_URL+ count + "/" + page;
ArrayList<Sister> sisters = new ArrayList<>();
try {
URL url = new URL(fetchUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(5000);
connection.setRequestMethod("GET");
int code = connection.getResponseCode();
Log.d(TAG,"Server response" + code);//测试成功
if (code == 200){
InputStream in = connection.getInputStream();
byte[] data = readFromStream(in);//将读取的数据以字节流的方式存储
String result = new String(data,"UTF-8");
sisters = parseSister(result);//得到解析后的sister
}else{
Log.d(TAG,"请求失败" + code);
}
} catch (Exception e) {
e.printStackTrace();
}
return sisters;
}
//解析返回的json数据的方法
private ArrayList<Sister> parseSister(String result) throws Exception {
ArrayList<Sister> sisters = new ArrayList<>();
JSONObject object = new JSONObject(result);
JSONArray array = object.getJSONArray("results");
for (int i=0;i<array.length();i++){
JSONObject results = (JSONObject) array.get(i);
Sister sister = new Sister();
sister.set_id(results.getString("_id"));
sister.setCreateAt(results.getString("createdAt"));
sister.setDesc(results.getString("desc"));
sister.setPublishedAt(results.getString("publishedAt"));
sister.setSource(results.getString("source"));
sister.setType(results.getString("type"));
sister.setUrl(results.getString("url"));
sister.setUsed(results.getBoolean("used"));
sister.setWho(results.getString("who"));
sisters.add(sister);
}
return sisters;
}
//读取流中数据的方法
private byte[] readFromStream(InputStream in) throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while((len = in.read(buffer)) != -1){
outStream.write(buffer,0,len);
}
in.close();
return outStream.toByteArray();
}
}
- bean
package com.example.bbw.gallery.entity;
/**
* Created by bbw on 2017/7/17.
* Bean
*/
public class Sister {
private String _id;
private String createAt;
private String desc;
private String publishedAt;
private String source;
private String type;
private String url;
private boolean used;
private String who;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getCreateAt() {
return createAt;
}
public void setCreateAt(String createAt) {
this.createAt = createAt;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean isUsed() {
return used;
}
public void setUsed(boolean used) {
this.used = used;
}
public String getWho() {
return who;
}
public void setWho(String who) {
this.who = who;
}
}