//2.声明接口变量
private ICallBacks iCallBacks;
//3.定义带参构造方法
public MyTask(ICallBacks iCallBacks) {
this.iCallBacks = iCallBacks;
}
@Override
protected String doInBackground(String... strings) {
String str="";
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
int responseCode = urlConnection.getResponseCode();
if (responseCode==200){
InputStream inputStream = urlConnection.getInputStream();
str=toJson(inputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//4.调用接口的方法
iCallBacks.getJsonData(s);
}
//1.定义一个内部接口
public interface ICallBacks{
//得到json串
void getJsonData(String jsonstr);
}
private String toJson(InputStream inputStream) {
StringBuilder builder = new StringBuilder();
String string;
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(in);
try {
while ((string=reader.readLine())!=null){
builder.append(string);
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
private ICallBacks iCallBacks;
//3.定义带参构造方法
public MyTask(ICallBacks iCallBacks) {
this.iCallBacks = iCallBacks;
}
@Override
protected String doInBackground(String... strings) {
String str="";
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
int responseCode = urlConnection.getResponseCode();
if (responseCode==200){
InputStream inputStream = urlConnection.getInputStream();
str=toJson(inputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//4.调用接口的方法
iCallBacks.getJsonData(s);
}
//1.定义一个内部接口
public interface ICallBacks{
//得到json串
void getJsonData(String jsonstr);
}
private String toJson(InputStream inputStream) {
StringBuilder builder = new StringBuilder();
String string;
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(in);
try {
while ((string=reader.readLine())!=null){
builder.append(string);
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
ViewHolder viewHolder = null;
if (view==null){
view= View.inflate(context, R.layout.item,null);
viewHolder = new ViewHolder1();
viewHolder.img = view.findViewById(R.id.img);
viewHolder.title = view.findViewById(R.id.title);
view.setTag(viewHolder);
}else{
viewHolder= (ViewHolder) view.getTag();
}
viewHolder.title.setText(results.get(i).getTitle());
//取出image网址
ImageLoader.getInstance().displayImage(results.get(i).getPicUrl(), viewHolder.img, options);
return view;