提取工具类
private void LoadDate(int page) {
final String path = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?page="+page+"&count=3";
String Urlwith = path+page;
new AsyncTask<String,String,List<Person.ResultDate>>(){
@Override
protected List<Person.ResultDate> doInBackground(String... strings) {
Person dian = null;
String httpcon = new Util().httpcon(strings[0]);
dian = new Gson().fromJson(httpcon,Person.class);
return dian == null ? null:dian.getResult();
}
@Override
protected void onPostExecute(List<Person.ResultDate> resultDates) {
super.onPostExecute(resultDates);
if(resultDates == null){
Toast.makeText(getActivity(),"请求数据失败",Toast.LENGTH_SHORT).show();
return;
}
updateData(resultDates);
loade();
canloadmore(resultDates.size() == 3);
}
}.execute(Urlwith);
}
创建新的工具类方法
package com.example.lxzk;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Util {
public String httpcon(String urls){
try {
URL url = new URL(urls);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setRequestMethod("GET");
int request = httpURLConnection.getResponseCode();
if(request == 200){
String str = String2(httpURLConnection.getInputStream());
return str;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String String2(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String text = null;
while ((text = bufferedReader.readLine()) != null){
stringBuilder.append(text);
}
bufferedReader.close();
return stringBuilder.toString();
}
}