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);
        
上一篇:vue post请求方式传参问题


下一篇:thinkphp5.0 微信小程序订阅消息