Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
984 views
in Technique[技术] by (71.8m points)

android - ProcessDialog is not appearing properly?

This is my function that is in LoginActivity.java.So onclick of button i am calling this function.

public  void postHttpRequest(String userId,String pass,TextView error){
        RequestClient reqClient = new RequestClient(LoginActivity.this);
        String AppResponse = null;
        try {
            url = "myurl";
            Log.d("URL", url);
            AppResponse = reqClient.execute().get();
            String status = ValidateLoginStatus.checkLoginStatus(AppResponse);
            Log.d("Status recived", status);

            if(status.equals("200")){
                saveInformation(userId,pass);
                startingActivity(HOST_URL);
            }else{
                error.setText("Incorrect UserName or Password");
            }
        } catch (Exception e) {
            Log.e("Exception Occured", "Exception is "+e.getMessage());
        }
    }

From this function i am calling a AsynkTask for Http Communication.So onclick of button when i am geeting the response then my processDialog in opening just for one sec.I want as i click the buttoon my processDialog should get open utill i got the response

public class RequestClient extends AsyncTask<String, Void, String>{
    ProgressDialog pDialog;
    Context context;

    public RequestClient(Context c) {
        context = c;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Authenticating user...");
        pDialog.show();

    }

    @Override
    protected String doInBackground(String... aurl){
    String responseString="";
    DefaultHttpClient httpClient=new DefaultHttpClient();
    try {
         HttpClient client = new DefaultHttpClient();  
         HttpGet get = new HttpGet(LoginActivity.url);
         HttpResponse responseGet = client.execute(get);  
         HttpEntity resEntityGet = responseGet.getEntity();  
         if (resEntityGet != null) {  
             responseString = EntityUtils.toString(resEntityGet);
             Log.i("GET RESPONSE", responseString);
         }
    } catch (Exception e) {
        Log.d("ANDRO_ASYNC_ERROR", "Error is "+e.toString());
    }
    Log.d("ANDRO_ASYNC_ERROR", responseString);
     httpClient.getConnectionManager().shutdown();
     return responseString;

    }


    @Override
    protected void onPostExecute(String response) {
         super.onPostExecute(response); 
            if(pDialog!=null) 
            pDialog.dismiss();
    }
}

So please suggest me what changes i have to make so that processDialog should display properly in the center of the device

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

//add style in your progressbialog

 protected void onPreExecute() {
            super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.setMessage("Authenticating user..."); 

if (pDialog != null && !pDialog.isShowing()) {
                        pDialog.show();
                    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...