如何调用HttpURLConnection的工具类
public class ZKActivity extends AppCompatActivity {
private ListView news;
private NewsAdapter mAdapter;
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zk);
news = findViewById(R.id.news);
mAdapter = new NewsAdapter(this);
news.setAdapter(mAdapter);
//news.setOnClickListener();
news.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ZKActivity.this, DetailActivity.class);
intent.putExtra(URL_EXTRA_DATA_KEY, mAdapter.getItem(position).getUrl());
startActivity(intent);
}
});
loadData();
}
private String ttUrl =
"http://........";
private String gnUrl =
"http://........";
@SuppressLint("StaticFieldLeak") private void loadData() {
new AsyncTask<String, Void, List<NewsResponseBean.NewsItem>>() {
@Override protected List<NewsResponseBean.NewsItem> doInBackground(String... strings) {
List<NewsResponseBean.NewsItem> result = new ArrayList<>();
//
for (String url:strings) {
NewsResponseBean bean = NetUtil.getRequest(url, NewsResponseBean.class);
//只有最后一组结果
//result = bean.getResult().getData();
result.addAll(bean.getResult().getData());
}
return result;
}
@Override protected void onPostExecute(List<NewsResponseBean.NewsItem> newsItems) {
mAdapter.setDatas(newsItems);
}
}.execute(ttUrl, gnUrl);
}
}
创建HttpURLConnection的工具类的地址:https://blog.csdn.net/weixin_43587850/article/details/83684875