//---------------------------------------------------这是一个类---------------------------------------------
public class NetUtils {
//内部私有修饰符
private static NetUtils instsance;
public NetUtils(){
}
//单例模式
public static NetUtils getInstsance(){
if(instsance==null){
instsance = new NetUtils();
}
return instsance;
}
//判断网络状态
public static boolean getNet(Context context){
ConnectivityManager service = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = service.getActiveNetworkInfo();
return networkInfo!=null&&networkInfo.isAvailable();
}
//定义借口
public static interface CallBacks<T>{
void onSuccess(T t);
}
//异步请求
public static void RequestData(final String jiekou,final Class classs,final CallBacks callBacks){
new AsyncTask<String, Void, Object>() {
@Override
protected Object doInBackground(String... strings) {
return RequestData1(jiekou,classs);
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
callBacks.onSuccess(o);
}
}.execute(jiekou);
}
public static <T> T RequestData1(String jiekou, Class classs) {
T t = (T) new Gson().fromJson(RequestData2(jiekou), classs);
return t;
}
public static String RequestData2(String jiekou) {
try {
URL url = new URL(jiekou);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setRequestMethod("GET");
byte[] bytes = new byte[1024];
int len = 0;
if(urlConnection.getResponseCode()==200){
InputStream inputStream = urlConnection.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while ((len=inputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}
String json = outputStream.toString();
inputStream.close();
outputStream.close();
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
//------------------------------------------这是一个页面----------------------------------------------------
public class Tab_frag_01 extends Fragment {
private String path = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
private List<Beans.DataBean> list = new ArrayList<>();
private PullToRefreshListView plistview;
private int num = 1;
private Handler handler = new Handler();
private MyAdapter myAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_frag_01,container,false);
//控件
plistview = view.findViewById(R.id.plistview1);
//支持上拉加载 下拉刷新
plistview.setMode(PullToRefreshListView.Mode.BOTH);
//上拉加载 下拉刷新的方法
initjias();
//解析的方法
RequstData(1);
return view;
}
private void initjias() {
plistview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
num = 1;
list.clear();
RequstData(1);
}
},3000);
myAdapter.notifyDataSetChanged();
plistview.onRefreshComplete(); //停止刷新
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
num++;
RequstData(num);
}
},3000);
myAdapter.notifyDataSetChanged();
plistview.onRefreshComplete(); //停止刷新
}
});
}
private void RequstData(int num) {
NetUtils.getInstsance().RequestData(path+num, Beans.class, new NetUtils.CallBacks<Beans>() {
@Override
public void onSuccess(Beans beans) {
list.addAll(beans.getData());
myAdapter = new MyAdapter(getActivity(), list);
plistview.setAdapter(myAdapter);
}
});
}
}
10-10
529
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-06
392
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)