package me.xiaopan.sketchsample.net.request;
import android.content.Context;
import com.google.gson.annotations.SerializedName;
import org.apache.http.HttpResponse;
import java.util.Iterator;
import java.util.List;
import me.xiaopan.gohttp.HttpRequest;
import me.xiaopan.gohttp.MethodType;
import me.xiaopan.gohttp.requestobject.Method;
import me.xiaopan.gohttp.requestobject.Param;
import me.xiaopan.gohttp.requestobject.Request;
import me.xiaopan.gohttp.requestobject.URL;
/**
* 明星
*/
@URL("http://image.baidu.com/data/star/listjson")
@Method(MethodType.GET)
public class StarImageRequest implements Request {
@Param("pn")
private int start = 0;
@Param("rn")
private int size = 60;
@Param("name")
private String name;
@Param("sorttype")
private int sorttype = 0;
@Param("p")
private String page = "star.home";
@Param("col")
private String col = "明星";
@Param("tag")
private String tag;
public StarImageRequest(String name) {
this.name = name;
this.tag = name;
}
public void setStart(int newStart) {
this.start = newStart;
}
public int getSize() {
return size;
}
public static class Response {
@SerializedName("totalNum")
private int total;
@SerializedName("return_number")
private int returnNumber;
@SerializedName("data")
private List<Image> images;
public int getTotal() {
return total;
}
public List<Image> getImages() {
return images;
}
}
public static class Image {
@SerializedName("thumbnail_url")
private String thumbUrl;
@SerializedName("image_url")
private String sourceUrl;
@SerializedName("image_width")
private int width;
@SerializedName("image_height")
private int height;
public String getThumbUrl() {
return thumbUrl;
}
public String getSourceUrl() {
return sourceUrl;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
public static class ResponseHandler implements HttpRequest.ResponseHandleCompletedAfterListener<Response> {
private Context context;
public ResponseHandler(Context context) {
this.context = context;
}
@Override
public Object onResponseHandleAfter(HttpRequest httpRequest, HttpResponse httpResponse, Response response, boolean b, boolean b2) throws Throwable {
// String jsonContent = FileUtils.readAssetFile(context, "images.json");
// response = new Gson().fromJson(jsonContent, StarImageRequest.Response.class);
if (response.getImages() != null && response.returnNumber < response.getImages().size()) {
Iterator<Image> iterator = response.getImages().iterator();
int number = 0;
while (iterator.hasNext()) {
iterator.next();
if (number++ >= response.returnNumber) {
iterator.remove();
}
}
}
return response;
}
}
}