封装网络请求,使用HttpUrlConnection+AsyncTask和 XListView上拉和下拉刷新

自定义一个类

public class HttpUtils {

public String s;

//创建一个方法
public void getpath(String path) {
    MyasyncTask myasyncTask = new MyasyncTask();
    myasyncTask.execute(path);

}

//自定义一个asyncTask
public class MyasyncTask extends AsyncTask<String, Integer, String> {

    //与子线程一样,做耗时工作,获取网络数据
    @Override
    protected String doInBackground(String... strings) {
        try {
            URL url = new URL(strings[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(3000);
            int code = connection.getResponseCode();
            if (code == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                String s = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
                return s;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //与主线程一样
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        getData.setString(s);
    }
}

//写一个接口
public interface GetData {
    void setString(String s);
}

private GetData getData;

//写一个监听,可让外部访问
public void getstring(GetData getData1) {
    getData = getData1;
}

}

//在Java中写,这其中包括上拉和下拉刷新

public class Fragment21 extends Fragment implements XListView.IXListViewListener {

private ViewPager pager2;
private XListView xListView1;
private int count = 1;
private String path = "http://www.xieast.com/api/news/news.php?page=";
private HttpUtils httpUtils;
private ArrayList<User.DataBean> list;
private Myadapter myadapter;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frag21, container, false);
    //初始化控件
    pager2 = view.findViewById(R.id.viewpager2);
    xListView1 = view.findViewById(R.id.xlistview1);

    //实现上拉和下拉的刷新
    xListView1.setPullRefreshEnable(true);
    xListView1.setPullLoadEnable(true);
    xListView1.setXListViewListener(this);

    list = new ArrayList<>();
    //获取数据
    getData(count);

    //设置监听
    myadapter = new Myadapter(getActivity(), list);
    xListView1.setAdapter(myadapter);

    return view;
}

private void getData(int count) {
    httpUtils = new HttpUtils();
    httpUtils.getpath(path + count);
    httpUtils.getstring(new HttpUtils.GetData() {
        @Override
        public void setString(String s) {
            Gson gson = new Gson();
            User user = gson.fromJson(s, User.class);
           // list.clear();
            list.addAll(user.getData());
            myadapter.getList(list);

        }
    });

}

@Override
public void onRefresh() {
    list.clear();
    getData(1);
    close();
}

@Override
public void onLoadMore() {
    count++;
    getData(count);
    close();
}

public void close() {
    xListView1.stopRefresh();
    xListView1.stopLoadMore();
}

}

//包括adapter

public class Myadapter extends BaseAdapter {
private Context mcontext;
private ArrayList<User.DataBean> list;

public Myadapter(Context mcontext, ArrayList<User.DataBean> list) {
    this.mcontext = mcontext;
    this.list = list;
}

public void getList(ArrayList<User.DataBean> list) {
    this.list = list;
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = View.inflate(mcontext, R.layout.list1_item1, null);
        holder.mtitle = convertView.findViewById(R.id.mtitle);
        holder.image1 = convertView.findViewById(R.id.image1);
        holder.image2 = convertView.findViewById(R.id.image2);
        holder.image3 = convertView.findViewById(R.id.image3);
        holder.mdate = convertView.findViewById(R.id.mdate);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    User.DataBean bean = list.get(position);
    holder.mtitle.setText(bean.getTitle());
    holder.mdate.setText(bean.getDate());
    Picasso.with(mcontext).load(bean.getThumbnail_pic_s()).into(holder.image1);
    Picasso.with(mcontext).load(bean.getThumbnail_pic_s02()).into(holder.image2);
    Picasso.with(mcontext).load(bean.getThumbnail_pic_s03()).into(holder.image3);
    return convertView;
}


class ViewHolder {
    TextView mtitle, mdate;
    ImageView image1, image2, image3;
}

}

猜你喜欢

转载自blog.csdn.net/wzj_8899174/article/details/83267800
今日推荐