Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带进程对话框的异步任务_Java_Android - Fatal编程技术网

Java 带进程对话框的异步任务

Java 带进程对话框的异步任务,java,android,Java,Android,我知道他们对此有很多答案,但我找不到确切的答案: 1) 当用户点击按钮时,显示一个进度对话框 2) 执行类AsyncTask并等待答案(这是使用HTTPUrlConnection的响应) 3) 关闭进度对话框 我尝试了很多东西,但是进度对话框没有“出现”。我的代码: public class MainActivity extends Activity implements OnTaskCompleted{ .. private ProgressDialog progress

我知道他们对此有很多答案,但我找不到确切的答案:

1) 当用户点击按钮时,显示一个进度对话框

2) 执行类AsyncTask并等待答案(这是使用HTTPUrlConnection的响应)

3) 关闭进度对话框

我尝试了很多东西,但是进度对话框没有“出现”。我的代码:

   public class MainActivity extends Activity implements OnTaskCompleted{
    ..
    private ProgressDialog progressDialog;
    private Button btnLogin;
    ..

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            btnLogin = (Button) findViewById(R.id.btnLogin);

            btnLogin.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    progressDialog = ProgressDialog.show(MainActivity.this,
                            "", "Scanning Please Wait", true);
                    try {
                         String param1 = "testParam1";
                         String param2 = "testParam2";
                                String response = new SyncHelper(MainActivity.this).execute("http://server.example.com/api", param1, param2).get(); //this way, my activity waits of the answer
                               Log.d(TAG, "Finished: " + response);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } catch (ExecutionException e) {
                                e.printStackTrace();
                            }
                        } else {
                            // user didn't entered username or password
                            Toast.makeText(getApplicationContext(),
                                    "Done",
                                    Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                    }
                }
            });
        }

        public void onTaskCompleted()
        {
            progressDialog.dismiss();
        }



public class SyncHelper extends AsyncTask<Object, Void, String>
{
..
    private OnTaskCompleted listener;
..
        protected String doInBackground(Object... url) {
            String response = "";
            try {
                response = getRequest((String) url[0],(String) url[1], (String) url[2]); //Here I make a HttpURLConnection
            } catch (IOException e) {
                e.printStackTrace();
            }
            return response;
        }

        @Override
        protected void onPreExecute() {
        }

        protected void onPostExecute(String result) {
            listener.onTaskCompleted();
        }
}
    public interface OnTaskCompleted{
        void onTaskCompleted();
    }
public类MainActivity扩展活动实现OnTaskCompleted{
..
私有进程对话;
私人按钮;
..
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnLogin=(按钮)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
progressDialog=progressDialog.show(MainActivity.this,
“,”扫描请稍候“,对);
试一试{
字符串param1=“testParam1”;
字符串param2=“testParam2”;
字符串响应=新建SyncHelper(MainActivity.this)。执行(“http://server.example.com/apiget();//这样,我的活动将等待答案
Log.d(标记“完成:+响应”);
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
}否则{
//用户未输入用户名或密码
Toast.makeText(getApplicationContext(),
“完成”,
Toast.LENGTH_LONG).show();
}
}捕获(例外e){
}
}
});
}
公共void onTaskCompleted()
{
progressDialog.disclose();
}
公共类SyncHelper扩展异步任务
{
..
私有OnTaskCompleted侦听器;
..
受保护的字符串doInBackground(对象…url){
字符串响应=”;
试一试{
response=getRequest((String)url[0],(String)url[1],(String)url[2]);//这里我建立了一个httpurl连接
}捕获(IOE异常){
e、 printStackTrace();
}
返回响应;
}
@凌驾
受保护的void onPreExecute(){
}
受保护的void onPostExecute(字符串结果){
listener.onTaskCompleted();
}
}
已完成ONTASKC的公共接口{
void onTaskCompleted();
}
公共类MainActivity扩展活动{
..
私人按钮;
..
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnLogin=(按钮)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
试一试{
字符串param1=“testParam1”;
字符串param2=“testParam2”;
新建SyncHelper(MainActivity.this)。执行(“http://server.example.com/api“,param1,param2);//这样,我的活动等待答案
Log.d(标记“完成:+响应”);
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
}否则{
//用户未输入用户名或密码
Toast.makeText(getApplicationContext(),
“完成”,
Toast.LENGTH_LONG).show();
}
}捕获(例外e){
}
}
});
}
公共类SyncHelper扩展异步任务
{
..
语境;
私营部门;
..
公共SyncHelper(上下文c)
{
上下文=c;
} 
@凌驾
受保护的void onPreExecute(){
pd=新进度对话框(上下文);
pd.setTitle(“处理…”);
设置消息(“请稍候”);
pd.可设置可取消(假);
pd.SetUndeterminate(真);
pd.show();
}
受保护的字符串doInBackground(字符串…url){
字符串响应=”;
试一试{
response=getRequest(url[0],url[1],url[2]);//这里我建立了一个HttpURLConnection
}捕获(IOE异常){
e、 printStackTrace();
}
返回响应;
}
受保护的void onPostExecute(字符串结果){
//在这里,您将得到字符串结果中的响应。
if(pd.isShowing())
pd.解散();
}
}

使用get时,使用AsyncTask没有任何意义。因为
get()
会阻塞UI线程,所以我们可能无法看到进度对话框。如果要将响应发送回
main活动
,请像使用beofre一样使用回调接口

按钮工作吗???空的
捕捉器(例外e)
看起来很危险。。。您至少应该打印一个日志,说明发生了异常,而不是忽略异常。也许这就是原因
public class MainActivity extends Activity{
    ..

    private Button btnLogin;
    ..

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            btnLogin = (Button) findViewById(R.id.btnLogin);

            btnLogin.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                                       try {
                         String param1 = "testParam1";
                         String param2 = "testParam2";
                                 new SyncHelper(MainActivity.this).execute("http://server.example.com/api", param1, param2); //this way, my activity waits of the answer
                               Log.d(TAG, "Finished: " + response);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } catch (ExecutionException e) {
                                e.printStackTrace();
                            }
                        } else {
                            // user didn't entered username or password
                            Toast.makeText(getApplicationContext(),
                                    "Done",
                                    Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                    }
                }
            });
        }





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


..

    Context context;
    private ProgressDialog pd;
..

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


@Override
        protected void onPreExecute() {
            pd = new ProgressDialog(context);
            pd.setTitle("Processing...");
            pd.setMessage("Please wait.");
            pd.setCancelable(false);
            pd.setIndeterminate(true);
            pd.show();
        }
        protected String doInBackground(String... url) {
            String response = "";
            try {
                response = getRequest(url[0], url[1], url[2]); //Here I make a HttpURLConnection
            } catch (IOException e) {
                e.printStackTrace();
            }
            return response;
        }


        protected void onPostExecute(String result) {

            // here you will be getting the response in String result.
                 if (pd.isShowing()) 
                            pd.dismiss();

        }
}