Implementing AsyncTask in a new class

118 Views Asked by At

I am trying to use asynctask to upload entity as in Google's tutorial Mobile Assistant https://cloud.google.com/developers/articles/how-to-build-mobile-app-with-app-engine-backend-tutorial/. When I put below code in MainActivity it works but if I try to use the code in an another activity (in this case MakeDesire.java) it doesn't work. Problems are

1) new DesireTask().execute().......... DesireTask is underlined and says create new class. 2) Eclipse doesn't let private class DesireTask()..... when click the underline "Illegal modifier for the local class DesireTask; only abstract or final is permitted" But this works in MainActivity.

Any help is appriciated. Thanks in advance.

package com.google.samplesolutions.mobileassistant;

import java.io.IOException;



import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.samplesolutions.mobileassistant.desireendpoint.Desireendpoint;
import com.google.samplesolutions.mobileassistant.desireendpoint.model.Desire;


public class MakeDesire extends Activity{

private OnTouchListener sendexit = null;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sss);

    Button button1 = (Button) findViewById(R.id.button1);
    EditText kategori = (EditText) findViewById(R.id.editText1);
    final String ktgri = kategori.getText().toString();
    EditText mal =(EditText) findViewById(R.id.editText2);
    final String malstring = mal.getText().toString();
    final CheckBox alrm = (CheckBox) findViewById(R.id.checkBox1);

    sendexit = new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event){
            if(event.getAction()== MotionEvent.ACTION_UP){
                new DesireTask().execute();
                finish();
                return true;
            }
            return false;
        }
};
button1.setOnTouchListener(sendexit);

class DesireTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {
        Desire desire = new com.google.samplesolutions.mobileassistant.desireendpoint.model.Desire();

        desire.setCtgry(ktgri);
        desire.setGood(malstring);
        desire.setAlarm(alrm.isChecked());

        Desireendpoint.Builder builder = new Desireendpoint.Builder(AndroidHttp.newCompatibleTransport(), new JacksonFactory(), null);
         builder = CloudEndpointUtils.updateBuilder(builder);

         Desireendpoint endpoint = builder.build();

         try {
                endpoint.insertDesire(desire).execute();
              } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }

        return null;
    }
}
}
}
1

There are 1 best solutions below

0
On BEST ANSWER

It looks like you are pasting the inner class inside of the onCreate method. Try cutting the Task declaration and body and pasting it outside of onCreate.