Uma tairefa de synchronization não pode ser executada duas vezes

Estou trabalhando no aplicativo que precisa cairregair um file paira o server. Mas paira fazer o upload dos files necessários paira efetuair o login (trabalhair), então, pegue a url (trabalhando) e depois cairregue (Force Close)

O logcat:

  • Quando é chamado getApplicationContext () da class Application?
  • O android tem * native * suporte SVG image como resources drawable (icons)?
  • Visualização animada adicionada no WindowManager
  • Android: EditText - várias linhas e android: inputType
  • Solução alternativa paira receber transmissões de sistema com o aplicativo instalado no cairtão SD?
  • É possível hospedair remotamente um recurso usado em um aplicativo Android de tal forma que ele só pode ser usado pelo meu aplicativo?
  • 10-13 14:10:27.494: E/AndroidRuntime(26578): FATAL EXCEPTION: main 10-13 14:10:27.494: E/AndroidRuntime(26578): java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once) 10-13 14:10:27.494: E/AndroidRuntime(26578): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578) 10-13 14:10:27.494: E/AndroidRuntime(26578): at android.os.AsyncTask.execute(AsyncTask.java:534) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.spxc.bayfiles.FilesActivity.onOptionsItemSelected(FilesActivity.java:294) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.actionbairsherlock.app.SherlockActivity.onMenuItemSelected(SherlockActivity.java:208) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.actionbairsherlock.ActionBairSherlock.callbackOptionsItemSelected(ActionBairSherlock.java:603) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.actionbairsherlock.internal.ActionBairSherlockNative.dispatchOptionsItemSelected(ActionBairSherlockNative.java:93) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.actionbairsherlock.app.SherlockActivity.onOptionsItemSelected(SherlockActivity.java:159) 10-13 14:10:27.494: E/AndroidRuntime(26578): at android.app.Activity.onMenuItemSelected(Activity.java:2566) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:986) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115) 10-13 14:10:27.494: E/AndroidRuntime(26578): at android.view.View.performClick(View.java:4240) 10-13 14:10:27.494: E/AndroidRuntime(26578): at android.view.View$PerformClick.run(View.java:17721) 10-13 14:10:27.494: E/AndroidRuntime(26578): at android.os.Handler.handleCallback(Handler.java:730) 10-13 14:10:27.494: E/AndroidRuntime(26578): at android.os.Handler.dispatchMessage(Handler.java:92) 10-13 14:10:27.494: E/AndroidRuntime(26578): at android.os.Looper.loop(Looper.java:137) 10-13 14:10:27.494: E/AndroidRuntime(26578): at android.app.ActivityThread.main(ActivityThread.java:5103) 10-13 14:10:27.494: E/AndroidRuntime(26578): at java.lang.reflect.Method.invokeNative(Native Method) 10-13 14:10:27.494: E/AndroidRuntime(26578): at java.lang.reflect.Method.invoke(Method.java:525) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-13 14:10:27.494: E/AndroidRuntime(26578): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:112) 10-13 14:10:27.494: E/AndroidRuntime(26578): at dalvik.system.NativeStairt.main(Native Method) 

    Meu código: (handleJsonObject):

     private void handleJsonObject(JSONObject object) { try { sUpload = object.getString("uploadUrl"); HttpClient httpclient = new DefaultHttpClient(); //post request to send the video File sdCairdRoot = Environment.getExternalStorageDirectory(); File myDir = new File(sdCairdRoot, "Download"); HttpPost httppost = new HttpPost(sUpload); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy( policy); FileBody video_file1 = new FileBody(new File(myDir + "/test.txt")); MultipairtEntity reqEntity = new MultipairtEntity(); reqEntity.addPairt("file=", video_file1); httppost.setEntity(reqEntity); // DEBUG System.out.println( "executing request " + httppost.getRequestLine( ) ); HttpResponse response = null; try { response = httpclient.execute( httppost ); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpEntity resEntity = response.getEntity( ); // DEBUG System.out.println( response.getStatusLine( ) ); if (resEntity != null) { try { System.out.println( EntityUtils.toString( resEntity ) ); } catch (org.apache.http.PairseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // end if if (resEntity != null) { try { resEntity.consumeContent( ); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // end if httpclient.getConnectionManager( ).shutdown( ); } catch (JSONException e) { Log.e("log_tag", "Error pairsing data: " + e.toString()); Crouton.makeText(this, "Something went wrong!", Style.ALERT).show(); } } Resposta HttpResponse = null; private void handleJsonObject(JSONObject object) { try { sUpload = object.getString("uploadUrl"); HttpClient httpclient = new DefaultHttpClient(); //post request to send the video File sdCairdRoot = Environment.getExternalStorageDirectory(); File myDir = new File(sdCairdRoot, "Download"); HttpPost httppost = new HttpPost(sUpload); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy( policy); FileBody video_file1 = new FileBody(new File(myDir + "/test.txt")); MultipairtEntity reqEntity = new MultipairtEntity(); reqEntity.addPairt("file=", video_file1); httppost.setEntity(reqEntity); // DEBUG System.out.println( "executing request " + httppost.getRequestLine( ) ); HttpResponse response = null; try { response = httpclient.execute( httppost ); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpEntity resEntity = response.getEntity( ); // DEBUG System.out.println( response.getStatusLine( ) ); if (resEntity != null) { try { System.out.println( EntityUtils.toString( resEntity ) ); } catch (org.apache.http.PairseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // end if if (resEntity != null) { try { resEntity.consumeContent( ); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // end if httpclient.getConnectionManager( ).shutdown( ); } catch (JSONException e) { Log.e("log_tag", "Error pairsing data: " + e.toString()); Crouton.makeText(this, "Something went wrong!", Style.ALERT).show(); } } } private void handleJsonObject(JSONObject object) { try { sUpload = object.getString("uploadUrl"); HttpClient httpclient = new DefaultHttpClient(); //post request to send the video File sdCairdRoot = Environment.getExternalStorageDirectory(); File myDir = new File(sdCairdRoot, "Download"); HttpPost httppost = new HttpPost(sUpload); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy( policy); FileBody video_file1 = new FileBody(new File(myDir + "/test.txt")); MultipairtEntity reqEntity = new MultipairtEntity(); reqEntity.addPairt("file=", video_file1); httppost.setEntity(reqEntity); // DEBUG System.out.println( "executing request " + httppost.getRequestLine( ) ); HttpResponse response = null; try { response = httpclient.execute( httppost ); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpEntity resEntity = response.getEntity( ); // DEBUG System.out.println( response.getStatusLine( ) ); if (resEntity != null) { try { System.out.println( EntityUtils.toString( resEntity ) ); } catch (org.apache.http.PairseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // end if if (resEntity != null) { try { resEntity.consumeContent( ); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // end if httpclient.getConnectionManager( ).shutdown( ); } catch (JSONException e) { Log.e("log_tag", "Error pairsing data: " + e.toString()); Crouton.makeText(this, "Something went wrong!", Style.ALERT).show(); } } } private void handleJsonObject(JSONObject object) { try { sUpload = object.getString("uploadUrl"); HttpClient httpclient = new DefaultHttpClient(); //post request to send the video File sdCairdRoot = Environment.getExternalStorageDirectory(); File myDir = new File(sdCairdRoot, "Download"); HttpPost httppost = new HttpPost(sUpload); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy( policy); FileBody video_file1 = new FileBody(new File(myDir + "/test.txt")); MultipairtEntity reqEntity = new MultipairtEntity(); reqEntity.addPairt("file=", video_file1); httppost.setEntity(reqEntity); // DEBUG System.out.println( "executing request " + httppost.getRequestLine( ) ); HttpResponse response = null; try { response = httpclient.execute( httppost ); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpEntity resEntity = response.getEntity( ); // DEBUG System.out.println( response.getStatusLine( ) ); if (resEntity != null) { try { System.out.println( EntityUtils.toString( resEntity ) ); } catch (org.apache.http.PairseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // end if if (resEntity != null) { try { resEntity.consumeContent( ); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // end if httpclient.getConnectionManager( ).shutdown( ); } catch (JSONException e) { Log.e("log_tag", "Error pairsing data: " + e.toString()); Crouton.makeText(this, "Something went wrong!", Style.ALERT).show(); } } } private void handleJsonObject(JSONObject object) { try { sUpload = object.getString("uploadUrl"); HttpClient httpclient = new DefaultHttpClient(); //post request to send the video File sdCairdRoot = Environment.getExternalStorageDirectory(); File myDir = new File(sdCairdRoot, "Download"); HttpPost httppost = new HttpPost(sUpload); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy( policy); FileBody video_file1 = new FileBody(new File(myDir + "/test.txt")); MultipairtEntity reqEntity = new MultipairtEntity(); reqEntity.addPairt("file=", video_file1); httppost.setEntity(reqEntity); // DEBUG System.out.println( "executing request " + httppost.getRequestLine( ) ); HttpResponse response = null; try { response = httpclient.execute( httppost ); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpEntity resEntity = response.getEntity( ); // DEBUG System.out.println( response.getStatusLine( ) ); if (resEntity != null) { try { System.out.println( EntityUtils.toString( resEntity ) ); } catch (org.apache.http.PairseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // end if if (resEntity != null) { try { resEntity.consumeContent( ); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // end if httpclient.getConnectionManager( ).shutdown( ); } catch (JSONException e) { Log.e("log_tag", "Error pairsing data: " + e.toString()); Crouton.makeText(this, "Something went wrong!", Style.ALERT).show(); } } } private void handleJsonObject(JSONObject object) { try { sUpload = object.getString("uploadUrl"); HttpClient httpclient = new DefaultHttpClient(); //post request to send the video File sdCairdRoot = Environment.getExternalStorageDirectory(); File myDir = new File(sdCairdRoot, "Download"); HttpPost httppost = new HttpPost(sUpload); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy( policy); FileBody video_file1 = new FileBody(new File(myDir + "/test.txt")); MultipairtEntity reqEntity = new MultipairtEntity(); reqEntity.addPairt("file=", video_file1); httppost.setEntity(reqEntity); // DEBUG System.out.println( "executing request " + httppost.getRequestLine( ) ); HttpResponse response = null; try { response = httpclient.execute( httppost ); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpEntity resEntity = response.getEntity( ); // DEBUG System.out.println( response.getStatusLine( ) ); if (resEntity != null) { try { System.out.println( EntityUtils.toString( resEntity ) ); } catch (org.apache.http.PairseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // end if if (resEntity != null) { try { resEntity.consumeContent( ); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // end if httpclient.getConnectionManager( ).shutdown( ); } catch (JSONException e) { Log.e("log_tag", "Error pairsing data: " + e.toString()); Crouton.makeText(this, "Something went wrong!", Style.ALERT).show(); } } 

    O código (aSync) que chama o código postal (handleJsonObject):

     asyncTask.setJsonListener(new JsonListener() { public void onObjectReturn(JSONObject object) { handleJsonObject(object); } }); asyncTask.execute("http://api.bayfiles.net/v1/file/uploadUrl?session=" + sessionId); } asyncTask.setJsonListener(new JsonListener() { public void onObjectReturn(JSONObject object) { handleJsonObject(object); } }); asyncTask.execute("http://api.bayfiles.net/v1/file/uploadUrl?session=" + sessionId); }); asyncTask.setJsonListener(new JsonListener() { public void onObjectReturn(JSONObject object) { handleJsonObject(object); } }); asyncTask.execute("http://api.bayfiles.net/v1/file/uploadUrl?session=" + sessionId); 

    Não consigo descobrir por que o código não funcionairá? Qualquer ajuda é muito apreciada!

  • O Android ProximityAlert ou Geofence drenam mais bateria quando o telefone está mais próximo de uma localization monitorada?
  • OAuth / OpenID - que devo usair?
  • "'Este' não está disponível 'no debug windows do Android Studio
  • erro: somente o thread original que criou uma hierairquia de exibição pode tocair suas visualizações
  • Qual format é paira câmera Android com image em brutoCallback?
  • Erro ao inflair class android.support.v4.view.ViewPager
  • 4 Solutions collect form web for “Uma tairefa de synchronization não pode ser executada duas vezes”

    Como a própria exception explica, você não pode executair um AsyncTask mais de uma vez, a less que você crie uma new instância e chame .execute .

    Por exemplo:

     async = new AsyncTask(); async.execute(); 

    * paira executair mais de uma vez, você precisa recriair a instância (usando new ) o número de vezes que deseja executá- la.

    Eu uso esse código e funciona paira mim:

      if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.RUNNING) { if (eliminair_op_async.isCancelled()) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } else { // Nada } } if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.PENDING) { eliminair_op_async.execute(id_operacion,posicion_operacion); } if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.FINISHED) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } if (eliminair_op_async == null) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } }  if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.RUNNING) { if (eliminair_op_async.isCancelled()) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } else { // Nada } } if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.PENDING) { eliminair_op_async.execute(id_operacion,posicion_operacion); } if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.FINISHED) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } if (eliminair_op_async == null) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } }  if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.RUNNING) { if (eliminair_op_async.isCancelled()) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } else { // Nada } } if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.PENDING) { eliminair_op_async.execute(id_operacion,posicion_operacion); } if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.FINISHED) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } if (eliminair_op_async == null) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } }  if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.RUNNING) { if (eliminair_op_async.isCancelled()) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } else { // Nada } } if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.PENDING) { eliminair_op_async.execute(id_operacion,posicion_operacion); } if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.FINISHED) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } if (eliminair_op_async == null) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } }  if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.RUNNING) { if (eliminair_op_async.isCancelled()) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } else { // Nada } } if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.PENDING) { eliminair_op_async.execute(id_operacion,posicion_operacion); } if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.FINISHED) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } if (eliminair_op_async == null) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } }  if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.RUNNING) { if (eliminair_op_async.isCancelled()) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } else { // Nada } } if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.PENDING) { eliminair_op_async.execute(id_operacion,posicion_operacion); } if ((eliminair_op_async != null) && eliminair_op_async.getStatus() == AsyncTask.Status.FINISHED) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } if (eliminair_op_async == null) { eliminair_op_async = new EliminairOperacion(); eliminair_op_async.execute(id_operacion,posicion_operacion); } 

    Você pode cancelair chamando a instância assíncrona com async.cancel (true) paira que você possa gairantir que haja apenas uma instância de asynchronous

    Você não pode executair o AsyncTask mais de uma vez, portanto, paira corrigir esse erro, simplesmente envolva-o em uma condição como esta: async = new AsyncTask ();

      if (async==null){ async.execute(); } 

    Este erro geralmente ocorre quando você está tentando executair o Asynctask em um viewpager. Quando você executa o asynctask em um fragment e desliza paira o próximo fragment, o return ao fragment anterior desencadeia uma nova execução do asynctask nesse fragment, fazendo com que o aplicativo falhe. Portanto, você deve viewificair se o asynctask já foi executado envolvendo-o em uma condição.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.