ProcessDialog没有显示正确? [英] ProcessDialog is not appearing properly?

查看:141
本文介绍了ProcessDialog没有显示正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的功能就是在LoginActivity.java.So的onclick按钮,我调用此函数。

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());
        }
    }

从这个功能我打电话一AsynkTask对于HTTP Communication.So按钮的onclick当我在短短一个sec.I开放geeting的响应,那么我processDialog想为我点击buttoon我processDialog应该拿开utill我有响应

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();
    }
}

所以,请建议我有什么变化,我必须做出这样processDialog应在中心设备的

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

推荐答案

<一个href="http://stackoverflow.com/questions/15952999/asynctask-return-value-only-after-using-get-method">AsyncTask仅使用get()方法

从上面的链接绘图

调用的AsyncTask 的get()方法将发生阻塞主线程,并等待结果返回。这实际上使使用的AsyncTask成为一个同步操作在这种情况下,有一个在使用AsyncTask的没有意义的。

Calling the get() method of AsyncTask will block the main thread and wait for the result to be returned. This effectively makes using an AsyncTask become a synchronous operation in which case there's no point in using an AsyncTask.

我能想到的使用的get()方法的唯一原因是从一个线程比主(UI)线程,虽然我想不出其他很多理由这样做。

The only reason I can think of to use the get() method would be from a thread other than the main (UI) thread although I can't think of many reasons to do that.

在按钮点击

       RequestClient reqClient = new RequestClient(LoginActivity.this,new TheInterface() {
             @Override
             public void theMethod(String result) {
                 Log.i("Result  =",result); 
            }  
        });
       reqClient.execute(url); // no get(). pass url to doInBackground()      

在您的活动类

    public interface TheInterface {  
    public void theMethod(String result);

       }
     }

的AsyncTask

AsyncTask

public class RequestClient extends AsyncTask<String, Void, String>{
    ProgressDialog pDialog;
    Context context;
    TheInterface listener; 
    public RequestClient(Context c,TheInterface listen) {
        context = c;
        listener = listen;   
    }

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

    }

    @Override
    protected String doInBackground(String... aurl){
    String responseString="";
     HttpClient client; 
    try {
         HttpClient client = new DefaultHttpClient();  
         HttpGet get = new HttpGet(aurl[0]); // 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);
     client.getConnectionManager().shutdown();
     return responseString;

    }


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

    }
}

这篇关于ProcessDialog没有显示正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆