Проблема с отображением ProgressDialog в Asynk Task - PullRequest
0 голосов
/ 12 июля 2020
• 1000 загрузки, пожалуйста, помогите мне, вот мой код
public class MainActivity extends AppCompatActivity {
ProgressDialog mProgressDialog;
  
    public class coronacontent extends AsyncTask<String,String,String>
    {String result="";

        @Override
        protected void onPreExecute() {

            mProgressDialog.show(MainActivity.this,"Loading","Please Wait",true);
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            mProgressDialog.setProgress(Integer.parseInt(values[0]));
        }

        @Override
        protected String doInBackground(String... strings) {
            URL url;
            HttpURLConnection urlConnection=null;

            try {
                url=new URL(strings[0]);
                urlConnection= (HttpURLConnection) url.openConnection();
                InputStream inputStream=urlConnection.getInputStream();
                InputStreamReader reader=new InputStreamReader(inputStream);
                int data=reader.read();
                while(data!=-1) {
                    char current= (char) data;
                    result+=current;
                    data=reader.read();
                }
                return  result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONObject jsonObject=new JSONObject(result);
                String dailyreport= jsonObject.getString("statewise");
                JSONArray jsonArray=new JSONArray(dailyreport);
                String state[]= new String[jsonArray.length()];
                String confirmed[]= new String[jsonArray.length()];
                String active[]= new String[jsonArray.length()];
                String recovered[]= new String[jsonArray.length()];
                String death[]= new String[jsonArray.length()];
                int i;
                for( i=0;i<jsonArray.length();i++) {
                    JSONObject jsonpart=jsonArray.getJSONObject(i);

                    state[i]=jsonpart.getString("state");
                    confirmed[i]=jsonpart.getString("confirmed");
                    active[i]=jsonpart.getString("active");
                    recovered[i]=jsonpart.getString("recovered");
                    death[i]=jsonpart.getString("deaths");

                    Log.i("data: ",state[i] + ":  cnf " +confirmed[i]+" act:"+active[i]+" rec:"+ 
                     recovered[i]+" dth:"+death[i]);
                }
            } catch (JSONException e) {
                e.printStackTrace();
        }
    }}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mProgressDialog = new ProgressDialog(MainActivity.this);
        // Set your progress dialog Title
        mProgressDialog.setTitle("Downloading");
        // Set your progress dialog Message
        mProgressDialog.setMessage("Downloading, Please Wait!");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    }
    public void onclick(View v)
    {mProgressDialog.show();
        coronacontent coroaconstructer=new coronacontent();
        String result;

        try{
            result=coroaconstructer.execute("https://api.covid19india.org/data.json").get();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...