Display Dialog when loading internet

亡梦爱人 提交于 2019-12-12 06:04:15

问题


I am trying to create a dialog when loading Httprequest. But it load during the i click to intent from last Activity, but not the start of this Activity.

And the dialog just shown in 0.00001sec then dismiss.

Am i implement it wrongly?

Here is my codes

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    HttpPostHandler2 handler = new HttpPostHandler2();
    String URL ="http://xxxxxx";        
    handler.execute(URL);
}

public class HttpPostHandler2 extends AsyncTask<String, Void, String> {

    private String resultJSONString = null;
    private ProgressDialog pDialog;

    public String getResultJSONString() {
        return resultJSONString;
    }

    public void setResultJSONString(String resultJSONString) {
        this.resultJSONString = resultJSONString;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please Wait");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {

        CredentialsProvider credProvider = new BasicCredentialsProvider();
        credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST,
                AuthScope.ANY_PORT), new UsernamePasswordCredentials("core",
                "core1234"));
        String responseContent = "";

        HttpClient httpClient = new DefaultHttpClient();
        ((AbstractHttpClient) httpClient).setCredentialsProvider(credProvider);
        HttpPost httpPost = new HttpPost(params[0]);

        HttpResponse response = null;
        try {
            // Execute HTTP Post Request
            response = httpClient.execute(httpPost);
            responseContent = EntityUtils.toString(response.getEntity());
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        setResultJSONString(responseContent);
        // return new JSONObject(responseContent);
        return responseContent;
    }

    @Override
    protected void onPostExecute(String result) {
        pDialog.dismiss();  
        super.onPostExecute(result);
        resultJSONString = result;
    }
}

回答1:


Make sure that the work of HttpPostHandler2 is long enough to display the pDialog. If it not, it will disappear really soon. However, you cannot display GUI in onCreate. To display the dialog, you should move them to onStart:

@Override
public void onCreate(Bundle savedInstanceState) {//GUI not ready: nothing is shown
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  HttpPostHandler2 handler = new HttpPostHandler2();

}

@Override
protected void onStart () {//GUI is ready
   String URL ="http://xxxxxx";
   handler.execute(URL);
}

See comment for more information.



来源:https://stackoverflow.com/questions/28755795/display-dialog-when-loading-internet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!