AsyncTask实现异步网络请求
作者:互联网
main主方法
public class MainActivity extends AppCompatActivity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
MyTask mytask=new MyTask();
mytask.execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1");
}
class MyTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
String str="";
try {
URL url=new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
标签:异步,请求,connection,lv,AsyncTask,params,MyTask,String 来源: https://blog.csdn.net/weixin_38107457/article/details/122050183