AsyncTask

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLVphone = findViewById(R.id.lv_content);

        mAdapter = new PhoneAdapter(this);
        mLVphone.setAdapter(mAdapter);

        findViewById(R.id.btn_query).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!NetUtil.hasNetwork(MainActivity.this)) {
                    showNotNetworkDialog();
                }

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        requestData();
                    }
                }).start();
            }


        });
    }

    private void showNotNetworkDialog() {
        new AlertDialog.Builder(this)
                .setTitle("没有可用网络")
                .setMessage("当前网络不可用,是否去设置")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Toast.makeText(MainActivity.this, "去设置", Toast.LENGTH_LONG).show();

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();

    }

    private String urlStr = "https://suggest.taobao.com/sug?code=utf-8&q=%E6%89%8B%E6%9C%BA";
  private final int REQUEST_DATA_SUCCESS =1;
  private final int SERA_DATA_SUCCESS = 2;
    @SuppressLint("StaticFieldLeak")
    private void requestData() {
      result = new AsyncTask>>() {
          @Override
          protected List> doInBackground(String... strings) {
              try {
                  HttpClient client = HttpClients.createDefault();
                  HttpUriRequest request = RequestBuilder
                          .get()
                          .setUri(strings[0])
                          .build();
                  HttpResponse response = client.execute(request);

                  if (response.getStatusLine().getStatusCode()== 200) {
                      String result = EntityUtils.toString(response.getEntity());
                      PhoneBean bean = new Gson().fromJson(result,PhoneBean.class);
                      publishProgress(REQUEST_DATA_SUCCESS);
                      publishProgress(SERA_DATA_SUCCESS);
                      return  bean.getResult();
                  }
              } catch (MalformedURLException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              return null;
          }

          @Override
          protected void onProgressUpdate(Integer... values) {
              Toast.makeText(MainActivity.this,
                      values[0] ==REQUEST_DATA_SUCCESS ?"请求数据成功":"解析数据成功",
                      Toast.LENGTH_SHORT).show();
          }

          @Override
          protected void onPostExecute(List> lists) {
              mAdapter.setmResult(lists);
          }
      }.execute(urlStr);
    }

 

你可能感兴趣的:(AsyncTask)