Manipulate onPostExecute from another Activity

Asked

Viewed 139 times

2

I have both classes below:
Dataload.java

public class DataLoad extends Activity {

    SQLiteDatabase mDatabase;
    Session session;

    public static String jsonString = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.data_load_activity);

        // OBJECTS
        CRUD crud = new CRUD(getApplicationContext());
        session = new Session(getApplicationContext());

        crud.destroy();
        if(!Functions.DatabaseExists(getApplicationContext().getDatabasePath(Config.DATABASE_NAME).toString())){
            try {
                crud.open();
                getAppData();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public void getAppData(){
        Webservices webservice = new Webservices(getApplicationContext());
        webservice.execute();
    }

    public void Builder(){

        session.insert("json_content", jsonString);

        Intent intent = new Intent(DataLoad.this, Login.class);
        startActivity(intent);
    }
}

Webservices.java

public class Webservices  extends AsyncTask<String,String,String> {

    Context mContext;
    ProgressDialog progressDialog;

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

    @Override
    protected String doInBackground(String... string) {

        String RETRIEVED_CONTENT = "";

        InputStream inputStream;

        try {
            // New HTTP Object
            HttpClient httpclient = new DefaultHttpClient();

            // New POST Object
            HttpPost httppost = new HttpPost(Config.DOMAIN+Config.WEBSERVICE_PATH+Config.DATA_FILE);

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            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).append("\n");
            }

            RETRIEVED_CONTENT = sb.toString();

        } catch (Exception e) {
            Log.e("HTTP", "Error in http connection " + e.toString());
        }

        DataLoad.jsonString = RETRIEVED_CONTENT;

        return RETRIEVED_CONTENT;
    }



    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(mContext, "Por favor aguarde...", "Validando dados de login.", true);
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        DataLoad dataLoad = new DataLoad(mContext);
        dataLoad.Builder();
    }

}

I need to be able to perform the function Builder of the Dataload class within the method onPostExecutebut I’m getting the following error:

7247-7247/pt.project01 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{pt.project01/pt.project01.ui.activities.DataLoad}: java.lang.InstantiationException: can't instantiate class pt.cartuxa_pro_gold.ui.activities.DataLoad; no empty constructor

How can I manipulate the method onPostExecute so that after executed I managed to call other functions within the class that activated it?

  • Take a look at this answer: http://answall.com/a/46976/20594

  • 1

    I don’t recommend instantiating a Activity (Dataload). To call class methods DataLoad at the end of the implementation of onPostExecute you need to implement the Pattern design Listener (Observer).

1 answer

1

I have a habit of doing the following:

public interface DataLoadListener(){
   public void Builder(); 
}

In the Dataload Class:

public class DataLoad extends Activity implements DataLoadListener {

...
@Override
public void Builder(){
    ....
}

In the webservice:

 private DataLoadListener dataListener;

 public Webservices(Context context, DataLoadListener dataListener){
    mContext = context;
    this.dataListener = DataLoadListener dataListener;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

    dataListener.Builder();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.