I have a method called getData()
that is loaded early in the splash activity ; it basically connects to json and retrieves some strings from it .
I have two problems :
My first problem here is ... This method takes a few seconds to connect and retrieve data from json but my program seems not to wait and continue playing the next lines on code and running the getData()
at the same time which is wrong . I need first to get some strings then play next code .
My second problem is that I want to call getData()
two times again if the app fails to connect to json . this can be checked with a Boolean called isupdated
that has a true value inside getData()
if it was successfully connected . But again , its impossible to call the method getData()
and wait it to finish .
None of these ways could help :
for(int x =0 , x<3 , x++ )
{
if(!jsonfetch.isupdated) {jsonfetch.getData(splash.this)}
}
or
while (!jsonfetch.isupdated){
jsonfetch.getData(splash.this);
}
The condition is always false and it calls the method simultaniously because it doesn't wait it to finish before checking the condition again ...
I have tried so many solutions like Putting getData()
on a thread and calling join()
but it doesn't wait . I have also tried AsyncTask but it calls onPostExecute()
before the method ends which I don't understand.
Bellow is getData() Code
public static void getData(final Context context) {
handleSSLHandshake();
requestQueue = Volley.newRequestQueue(context.getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, encryption.URL_main(), null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.i("update_statut","Start Fetch .");
popup_msg = jsonfect_Decryption(response.getString(encryption.epopup_msg));
banner = jsonfect_Decryption(response.getString(encryption.ebanner));
banner2= jsonfect_Decryption(response.getString(encryption.ebanner2));
interstitial = jsonfect_Decryption(response.getString(encryption.einterstitial));
interstitial_Inside = jsonfect_Decryption(response.getString(encryption.einterstitial_Inside));
youtube_url = jsonfect_Decryption(response.getString(encryption.eyoutube_url));
dialog_check = Boolean.parseBoolean(jsonfect_Decryption(response.getString(encryption.dialog_check)));
dialog_Image_URL =jsonfect_Decryption(response.getString(encryption.dialog_Image_URL));
dialog_URL = jsonfect_Decryption(response.getString(encryption.dialog_URL));
dialog_txt= jsonfect_Decryption(response.getString(encryption.dialog_msg));
dialog_canceble= Boolean.parseBoolean(jsonfect_Decryption(response.getString(encryption.dialog_canceble)));
m3u_app_url = jsonfect_Decryption(response.getString(encryption.edownload_appurl));
premium_url = jsonfect_Decryption(response.getString(encryption.emuimerp));
premium_msg = jsonfect_Decryption(response.getString(encryption.epremium_msg));
online_version=response.getString("ON_version");
updated_version=response.getString("UP_version");
url_version = jsonfect_Decryption(response.getString(encryption.eurl_version));
activation = response.getString("act");
isupdated=true;
Log.i("update_statut","Ending Fetch .");
} catch (JSONException e) {
e.printStackTrace();
Log.i("json",""+e.getMessage());
isupdated=false;
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("json",""+error.getMessage());
isupdated=false;
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
String creds = String.format("%s:%s",encryption.USERNAME(),encryption.Password());
String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
params.put("Authorization", auth);
return params;
}
};
requestQueue.add(jsonObjectRequest);
}
Bellow is the Splash Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
setContentView(R.layout._splash_java);
/// Start Animation
circle=(ImageView)findViewById(R.id.img_circle);
textView2=(TextView) findViewById(R.id.textView2);
txtwelcome=(TextView) findViewById(R.id.txtwelcome);
relativeLayout=(RelativeLayout)findViewById(R.id.relativelayout);
startAnimation();
// GET DATABASE DATA
while (!jsonfetch.isupdated){
jsonfetch.getData(splash.this);
}
// Check Version
onlineVersion = jsonfetch.online_version;
updatedVersion = jsonfetch.updated_version;
try {
currentVersion = splash.this.getPackageManager()
.getPackageInfo(splash.this.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
Log.d("update_statut", "currentVersion value is " + e.getMessage());
}
if ( onlineVersion == null || updatedVersion == null || onlineVersion.equals("null") || updatedVersion.equals("null") ) {
Log.d("update_statut", "online_Version or updated_Version issue");
Log.d("update_statut", "Current version " + currentVersion + " & Online version " + onlineVersion + " & Updated version " + updatedVersion);
check_version = true;
jsonfetch.isupdated = false;
} else if ((currentVersion.equals(onlineVersion))) {
Log.d("update_statut", "match");
Log.d("update_statut", "Current version " + currentVersion + " & Online version " + onlineVersion);
check_version = true;
Interstitial_LOAD();
} else if ((currentVersion.equals(updatedVersion))) {
Log.d("update_statut", "match");
Log.d("update_statut", "Current version " + currentVersion + " & Updated version " + updatedVersion);
check_version = true;
Interstitial_LOAD();
} else {
Log.d("update_statut", "doesn't match");
Log.d("update_statut", "Current version " + currentVersion + " & Online version " + onlineVersion);
check_version = false;
}
/// Load Next Activity after 5s so that Interstitial can be loaded
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if( (!check_version) && jsonfetch.isupdated){
update_dialog();
Log.d("update_statut", "Update dialog ");
} else {
Interstitial_SHOW();
}
}
}, 5000);
}
private void Interstitial_LOAD() {
String inter_code = jsonfetch.interstitial;
if ( inter_code!=null && !(inter_code.equals("null") || inter_code.equals("") ) ){
AdRequest adRequestI = new AdRequest.Builder().build();
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(inter_code);
interstitial.loadAd(adRequestI);
}
}
private void Interstitial_SHOW() {
String inter_code = jsonfetch.interstitial;
if ( inter_code!=null && !(inter_code.equals("null") || inter_code.equals("") ) )
{
if (interstitial.isLoaded())
{
interstitial.show();
interstitial.setAdListener(new AdListener(){
@Override
public void onAdClosed() {
startActivity(new Intent(splash.this, first_java.class).setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
splash.this.finish();
}
});
}
else
{
startActivity(new Intent(splash.this, first_java.class).setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
splash.this.finish();
}
}
else {
startActivity(new Intent(splash.this, first_java.class).setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
splash.this.finish();
}
}