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 onPostExecute
but 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
– André Ribeiro
I don’t recommend instantiating a
Activity
(Dataload). To call class methodsDataLoad
at the end of the implementation ofonPostExecute
you need to implement the Pattern designListener
(Observer).– Wakim