AsyncTask和onPreExecute

时间:2016-02-23 15:45:36

标签: java android android-studio

当我运行下面的AsyncTask时,我可以看到日志消息。然而,进度条不会改变可见度。

下面是我的AsyncTask代码。

class test extends AsyncTask<String, Void, String> {


    private Context mContext;

    public PostWav(Context context) {
        mContext = context;
    }


    protected String doInBackground(String... params ) {
        URL url= null;
        try {
            url = new URL(params[0]);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        InputStream inputStream;

        DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());


        JSONObject object = new JSONObject();
        try {
            object.put("provider_id",1);


            String message = object.toString();
            HttpPost httppost = new HttpPost(url.toString());

            httppost.setEntity(new StringEntity(message, "UTF8"));

            httppost.setHeader("Content-type", "application/json");
            httppost.setHeader("Accept", "application/json");
            String boundary = "-------------" + System.currentTimeMillis();
            httppost.setHeader("Content-type", "multipart/form-data; boundary="+boundary);

            File file = getDir("test", Context.MODE_PRIVATE);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();

            File wavFile = new File(file.getPath() + "/" + params[3]);

            ContentType contentType = ContentType.create("audio/x-wav");
            FileBody cbFile = new FileBody(wavFile,contentType,"sample");
            builder.addPart("sample", cbFile);
            builder.setBoundary(boundary);
            httppost.setEntity(builder.build());
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            Header[] header =   response.getHeaders("Location");
            String location = String.valueOf(header[0].getValue());
            inputStream = entity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }

            return location;

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } 
        return null;
    }
    @Override
    protected void onPreExecute() {
        Log.i("Async-Example", "onPreExecute Called");
        loading.setVisibility(View.VISIBLE);
        record.setVisibility(View.INVISIBLE);
        super.onPreExecute();

    }

    @Override
    protected void onPostExecute(String feed) {
        Log.i("Async-Example", "onPostExecute Called");

        loading.setVisibility(View.INVISIBLE);
        record.setVisibility(View.VISIBLE);

    }
}

我正在将.wav文件上传到我的API。我没有包括整个代码,这基本上就是我正在做的事情。

3 个答案:

答案 0 :(得分:1)

@greenapps谢谢你的提示你的提示足以让我理解这个问题。

基本上当我从另一个项目导入这个类时,我完全忘记了她编程的原因。 我只需要从execute命令中删除.get()并将方法传递给Task上的post执行,并且顺利完成所有操作。

我很抱歉占用了每个人的时间,我非常感谢你的帮助!

以下是我要找的结果:

enter image description here enter image description here

答案 1 :(得分:0)

您希望在任务完成时显示进度条,因此您需要解决此问题。

尝试使用以下方法完成工作:

  • doInBackground(int Progress)
  • OnProgressUpdate()根据您的想法更新进度条

如需更多阅读following documentation

答案 2 :(得分:0)

通常AsyncTask在UI线程上运行,但是我强烈假设您从UI线程之外的其他地方调用它,因为您将Context作为参数传递。
所以你可以使用它。

@Override
   protected void onPostExecute(String feed) {
         Log.i("Async-Example", "onPostExecute Called");
         activity.runOnUiThread(new Runnable(){
              loading.setVisibility(View.INVISIBLE);
              record.setVisibility(View.VISIBLE);
         });


    }

同时删除您不需要的super.onPreExecute(); 您必须将构造函数参数从Context更改为Activity,否则会更复杂。