为什么我的AsyncTask不会停止?

时间:2013-11-19 19:51:56

标签: android android-asynctask

我正在尝试将照片上传到我的FTP服务器,它没问题,但我的AsyncTask没有完成。我的日志显示一切正常。这是怎么回事? 这是我的代码:

  public class LoadPerfil extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Perfil.this);
        pDialog.setMessage("Carregando informações...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected Boolean doInBackground(Void... arg0) {
        //we will develop this method in version 2
        updateJSONdata();
        return null;

    }


    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        getSupportActionBar().setTitle(nome);
        Picasso.with(getApplicationContext()).
        load("http://gargsolutions.com.br/budge_backend/fotos/"+id+".jpg")
        .resize(200, 260)
        .centerCrop()
        .into(FotoPerfil);
        txtBiografia.setText(biografia);
        txtPontos.setText("Pontuação: "+String.valueOf(pontuacao));
        pDialog.dismiss();
      //we will develop this method in version 2



    }
}    

public class UploadFoto extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Perfil.this);
        pDialog.setMessage("Upando foto...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected Boolean doInBackground(Void... arg0) {
        //we will develop this method in version 2
          upaFoto();
        //upaMetodo2();

        return null;

    }


    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
      new LoadPerfil().execute();
      //we will develop this method in version 2
      pDialog.dismiss();


    }
}    

public void upaFoto(){

      FTPClient con = null;

      try
      {
          con = new FTPClient();
          con.connect("xxx");
          Log.i("ta chegando aqui", "antes do if de login");
          if (con.login("xx", "glaxxrgx"))
          {    
              Log.i("teste", "logou ftp");
              con.enterLocalPassiveMode(); // important!
              con.setFileType(FTP.BINARY_FILE_TYPE);
              String data1 = diretorio+"/"+id+".jpg";
              Log.i("teste", data1);

              FileInputStream in = new FileInputStream(new File(data1));
              boolean result = con.storeFile("/www/budge_backend/fotos/"+id+".jpg", in);
              in.close();
              if (result) Log.i("upload result", "succeeded");
              con.logout();
              con.disconnect();
          }
      }
      catch (Exception e)
      {
          Log.i("erro ao logar", e.toString());
          e.printStackTrace();
      }
}
public void updateJSONdata(){

// Instantiate the arraylist to contain all the JSON data.
// we are going to use a bunch of key-value pairs, referring
// to the json element name, and the content, for example,
// message it the tag, and "I'm awesome" as the content..

mPerfilLists = new ArrayList<HashMap<String, String>>();

// Bro, it's time to power up the J parser 
JSONParser jParser = new JSONParser();
// Feed the beast our comments url, and it spits us
//back a JSON object.  Boo-yeah Jerome.
LoadPrefs();
JSONObject json = jParser.getJSONFromUrl(LER_LOCAL_URL+"?email="+email);
Log.i("teste url", LER_LOCAL_URL+"?email="+email);

//when parsing JSON stuff, we should probably
//try to catch any exceptions:
try {

    //I know I said we would check if "Posts were Avail." (success==1)
    //before we tried to read the individual posts, but I lied...
    //mComments will tell us how many "posts" or comments are
    //available
    mPerfil = json.getJSONArray(TAG_POSTS);  
    // looping through all posts according to the json object returned
    for (int i = 0; i < mPerfil.length(); i++) {
        JSONObject c = mPerfil.getJSONObject(i);

        //gets the content of each tag
         nome = c.getString("nome");
         biografia = c.getString("biografia");
         pontuacao = c.getInt("pontuacao");
         id = c.getString("id");
        //String comentarios =  c.getString("coments");
        //String message = c.getString("sucess");
         Log.i("testando resultados: ", nome+biografia+pontuacao);


      // map.addMarker(new MarkerOptions()
        //.position(new LatLng(lati, longi))
        //.title(linha)
        //.snippet(usuario));


        //annndddd, our JSON data is up to date same with our array list
    }

} catch (JSONException e) {
    e.printStackTrace();
    Log.i("try/catch", "Ta caindo no cacth");
   // resultado = 1;
}
 }

  @Override
    protected void onResume() {
       super.onResume();
       new LoadPerfil().execute();
   }

编辑:

我的日志结果,你可以看到,“上传成功”,“在后面做......”,“在帖子上”:

11-19 18:25:41.192: I/teste url(2183): http://gargsolutions.com.br/budge_backend/perfil.php?email=Gabrielmalinosqui@gmail.com
11-19 18:25:41.192: I/testando resultados:(2183): Gabriel Jacomini Malinosqui18 anos, CEO at GARG Solutions, UNIP Ciência da Computação0
11-19 18:25:41.282: W/IMGSRV(2183): :0: gralloc_unregister_buffer: ID: 28956 handle: 0x56f3abf0 size: 513 x 144 fmt: 5 usage: 0xb00
11-19 18:25:41.282: W/IMGSRV(2183): :0: gralloc_unregister_buffer: ID: 28958 handle: 0x57324a20 size: 513 x 144 fmt: 5 usage: 0xb00
11-19 18:25:41.282: W/IMGSRV(2183): :0: gralloc_unregister_buffer: ID: 28961 handle: 0x5691b870 size: 513 x 144 fmt: 5 usage: 0xb00
11-19 18:25:41.412: W/IMGSRV(2183): :0: gralloc_unregister_buffer: ID: 28949 handle: 0x57224d00 size: 540 x 778 fmt: 511 usage: 0x300
11-19 18:25:42.292: W/IMGSRV(2183): :0: gralloc_unregister_buffer: ID: 28951 handle: 0x573aa2b0 size: 480 x 205 fmt: 1 usage: 0x933
11-19 18:25:44.482: W/IMGSRV(2183): :0: gralloc_unregister_buffer: ID: 28957 handle: 0x56f302b0 size: 540 x 888 fmt: 5 usage: 0xb00
11-19 18:25:44.482: W/IMGSRV(2183): :0: gralloc_unregister_buffer: ID: 28959 handle: 0x54e09450 size: 540 x 888 fmt: 5 usage: 0xb00
11-19 18:25:44.482: W/IMGSRV(2183): :0: gralloc_unregister_buffer: ID: 28960 handle: 0x57220de0 size: 540 x 888 fmt: 5 usage: 0xb00
11-19 18:25:51.632: I/Teste arquivo(2183): /storage/sdcard0/BusaoBauru/62.jpg
11-19 18:25:52.242: W/IMGSRV(2183): :0: gralloc_register_buffer: ID: 28974 handle: 0x5694a510 size: 513 x 144 fmt: 5 usage: 0xb00
11-19 18:25:52.292: W/IMGSRV(2183): :0: gralloc_register_buffer: ID: 28975 handle: 0x52f24e30 size: 540 x 888 fmt: 5 usage: 0xb00
11-19 18:25:52.312: W/IMGSRV(2183): :0: gralloc_register_buffer: ID: 28976 handle: 0x52f5df60 size: 513 x 144 fmt: 5 usage: 0xb00
11-19 18:25:52.472: W/IMGSRV(2183): :0: gralloc_register_buffer: ID: 28977 handle: 0x56d144c0 size: 513 x 144 fmt: 5 usage: 0xb00
11-19 18:25:52.582: W/IMGSRV(2183): :0: gralloc_register_buffer: ID: 28978 handle: 0x5798c6b0 size: 540 x 888 fmt: 5 usage: 0xb00
11-19 18:25:52.602: W/IMGSRV(2183): :0: gralloc_register_buffer: ID: 28979 handle: 0x54e01b80 size: 513 x 144 fmt: 5 usage: 0xb00
11-19 18:25:52.632: W/IMGSRV(2183): :0: gralloc_register_buffer: ID: 28980 handle: 0x52f5eac0 size: 513 x 144 fmt: 5 usage: 0xb00
11-19 18:25:52.662: W/IMGSRV(2183): :0: gralloc_register_buffer: ID: 28981 handle: 0x55656e10 size: 513 x 144 fmt: 5 usage: 0xb00
11-19 18:25:52.962: I/ta chegando aqui(2183): antes do if de login
11-19 18:25:53.392: I/teste(2183): logou ftp
11-19 18:25:53.522: W/IMGSRV(2183): :0: gralloc_register_buffer: ID: 28982 handle: 0x56d136d0 size: 540 x 888 fmt: 5 usage: 0xb00
11-19 18:25:53.582: I/teste(2183): /storage/sdcard0/BusaoBauru/62.jpg
11-19 18:25:55.152: I/upload result(2183): succeeded
11-19 18:25:55.472: I/teste(2183): do in back...
11-19 18:25:55.542: I/teste(2183): On post..
11-19 18:25:55.582: W/IMGSRV(2183): :0: gralloc_unregister_buffer: ID: 28976 handle: 0x52f5df60 size: 513 x 144 fmt: 5 usage: 0xb00
11-19 18:25:55.582: W/IMGSRV(2183): :0: gralloc_unregister_buffer: ID: 28979 handle: 0x54e01b80 size: 513 x 144 fmt: 5 usage: 0xb00
11-19 18:25:55.582: W/IMGSRV(2183): :0: gralloc_unregister_buffer: ID: 28981 handle: 0x55656e10 size: 513 x 144 fmt: 5 usage: 0xb00
11-19 18:25:56.162: I/teste url(2183): http://gargsolutions.com.br/budge_backend/perfil.php?email=Gabrielmalinosqui@gmail.com
11-19 18:25:56.162: I/testando resultados:(2183): Gabriel Jacomini Malinosqui18 anos, CEO at GARG Solutions, UNIP Ciência da Computação0

1 个答案:

答案 0 :(得分:0)

这个问题很可能是由于围绕AsyncTask的取消方法实际上做了什么的混淆。基本上你需要将后台任务分解为可循环段,然后在每个循环迭代开始执行任务之前,你需要检查任务是否被取消,然后如果你需要打破循环。似乎没有任何其他方法可以阻止执行AsyncTask。

我已经在此处发布了有关此问题的详细指南:

http://tpbapp.com/android-development/android-asynctask-stop-running-cancel-method/