android - 如何在 HTTP POST 上显示进度条?

标签 android multithreading http-post handler

我正在尝试将视频上传到 API,我想知道您如何显示进度条并在上传完成后关闭它?

public class Loadvid extends AsyncTask <Object,Integer,String>{
        EditText etxt_user = (EditText) findViewById(R.id.user_email);
        EditText etxt_pass = (EditText) findViewById(R.id.friend_email);

        protected void onPreExecute(){
            dialog = new ProgressDialog(share.this);
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setMax(100);
            dialog.show();
            super.onPreExecute();

        }

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

            if(etxt_user.equals("")||etxt_pass.equals("")){
                dialog.dismiss();
                Alert.setMessage("Please fill in the Blanks");
                Alert.show();

            }
else{
        Pattern keys= Pattern.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
                        "\\@" +
                        "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
                        "(" +
                        "\\." +
                        "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
                        ")+");
            Matcher matcher = keys.matcher((CharSequence) etxt_user);
            Matcher matcher1 = keys.matcher((CharSequence) etxt_pass);
            if(!matcher.matches()|| !matcher1.matches())
{
                    dialog.dismiss();
                    Alert.setMessage("Wrong email address.");
                    Alert.show();

                }
            }

            //implement the httppost.
        try
            {

 MultipartEntity me = new MultipartEntity();

 me.addPart("file", new FileBody(new File("/")));

 httppost.setEntity(me);

 HttpResponse responsePOST = client.execute(httppost);  
 HttpEntity resEntity = responsePOST.getEntity(); 

 InputStream inputstream = resEntity.getContent();
 BufferedReader buffered = new BufferedReader(new InputStreamReader(inputstream));
 StringBuilder stringbuilder = new StringBuilder();
 String currentline = null; 

 while ((currentline = buffered.readLine()) != null)
    { 
     stringbuilder.append(currentline + "\n"); 
     String result = stringbuilder.toString(); 
     Log.v("HTTP UPLOAD REQUEST",result); 
     inputstream.close();}  
}

     catch (Exception e) {
     e.printStackTrace();

    }


            return null;
        }


        protected void onPostExecute(String result){

            super.onPostExecute(result);

            if(result==null){
                dialog.dismiss();
                Alert.setMessage("The result failed");
                Alert.show();
                return;
            }
            else
            {

                Intent intent = new Intent("com...");
                startActivity(intent);
            }
        }


    }

但它不起作用。当我点击发送按钮时,它会显示处理程序 2 秒,然后关闭错误。

错误日志告诉我我的错误是在

Matcher matcher = keys.matcher((CharSequence) etxt_user);

@“在后台做”

在我的 onPrexecute 的某个地方。

我做错了什么,请帮忙!

最佳答案

只需使用 AsyncTask.它专为此类任务而设计:在后台执行长时间运行的代码并正确更新 UI(在 UI 线程上)。

关于android - 如何在 HTTP POST 上显示进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7561305/

相关文章:

java - 如果 Activity 位于图书馆项目中,则未找到 Activity 异常

java - 连接到google fit android API不在 Activity 类中

java - 在 Java 中停止线程的安全方法

javascript - 在 webview 中显示 android 原生日期选择器

android - 随意拖动手指

java - 关于 wait 和 notifyAll

java - 从 POST 保存分割上传的文件最终会变成空

javascript - 使用 AngularJS,我不想设置全局 $http.defaults.headers.common。我可以在每次调用 $resource 时发送我的自定义 header 吗?

c# - 具有多个参数的 ASP.NET Web 服务 HttpPut

java - 如何在 Mac OS 上设置 Appium 以在 Android 和 iOS 设备上运行来自 JAVA 类的自动化测试