ASync task cannot be executed twice

I am working on an application that should upload a file to the server. But to download files, he needs to log in (work), then get the URL (working), and then download (Force Close)

Log Code:

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.actionbarsherlock.app.SherlockActivity.onMenuItemSelected(SherlockActivity.java:208) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:603) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchOptionsItemSelected(ActionBarSherlockNative.java:93) 10-13 14:10:27.494: E/AndroidRuntime(26578): at com.actionbarsherlock.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.NativeStart.main(Native Method) 

My code: (handleJsonObject):

 private void handleJsonObject(JSONObject object) { try { sUpload = object.getString("uploadUrl"); HttpClient httpclient = new DefaultHttpClient(); //post request to send the video File sdCardRoot = Environment.getExternalStorageDirectory(); File myDir = new File(sdCardRoot, "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")); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("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.ParseException 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 parsing data: " + e.toString()); Crouton.makeText(this, "Something went wrong!", Style.ALERT).show(); } } 

Code (aSync) that calls the postal code (handleJsonObject):

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

I can’t understand why the code will not work? Any help is much appreciated!

+8
java json android android-asynctask
source share
4 answers

As the exception itself is explained, you cannot run AsyncTask more than once, unless you create an instance of new and call .execute .

For example:

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

* to execute several times, you need to recreate an instance (using new ) the number of times you want to execute it.

+28
source share

I use this code and it works for me:

  if ((eliminar_op_async != null) && eliminar_op_async.getStatus() == AsyncTask.Status.RUNNING) { if (eliminar_op_async.isCancelled()) { eliminar_op_async = new EliminarOperacion(); eliminar_op_async.execute(id_operacion,posicion_operacion); } else { // Nada } } if ((eliminar_op_async != null) && eliminar_op_async.getStatus() == AsyncTask.Status.PENDING) { eliminar_op_async.execute(id_operacion,posicion_operacion); } if ((eliminar_op_async != null) && eliminar_op_async.getStatus() == AsyncTask.Status.FINISHED) { eliminar_op_async = new EliminarOperacion(); eliminar_op_async.execute(id_operacion,posicion_operacion); } if (eliminar_op_async == null) { eliminar_op_async = new EliminarOperacion(); eliminar_op_async.execute(id_operacion,posicion_operacion); } 
0
source share

You can cancel by invoking the async instance with async.cancel (true) so that you can verify that there is only one async instance

0
source share

You cannot execute AsyncTask more than once, therefore, to fix this error, simply wrap it in this state: async = new AsyncTask ();

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

This error usually occurs when you try to run Asynctask in the viewpager. When you start the synthesis in a fragment and swipe the screen to the next fragment, returning to the previous fragment causes the synthesis to be repeated in this fragment, which leads to the failure of the application. Therefore, you need to check whether asynchronization has already been started by wrapping it in a state.

-one
source share

All Articles