Create a new class and add this code
Code:
public class AppConfig {
public static final String JSON_ARRAY = "result";
public static final String ads ="ads";
// flag for display ads
public static boolean ENABLE_Ads = false;
public static void getJSON(String url, Context context) {
StringRequest stringRequest = new StringRequest(url, new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
Log.i(ActivityPostDetails.TAG,"response: "+ response);
}
},
new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);
}
private static void showJSON(String response){
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(AppConfig.JSON_ARRAY);
JSONObject ads = result.getJSONObject(0);
if (collegeData.has("ads")){
ENABLE_Ads = ads.getBoolean("ads");
}
} catch (JSONException e) {
e.printStackTrace();
Log.d("getdata: ","error: ",e);
}
Log.i("gatadata ; ","ads: "+ads);
}
}
And then call this code to enable or disable ads
Code:
AppConfig.getJSON("http://yoursite.com/ads.txt",this);
And now when you set your ads it's time to set ads. Add this wherever you want your ads.
Code:
if (AppConfig.ENABLE_Ads
) {
AdView mAdView = (AdView) findViewById(R.id.ad_view);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
// Start loading the ad in the background.
mAdView.loadAd(adRequest);
} else {
((RelativeLayout) findViewById(R.id.banner_layout)).setVisibility(View.GONE);
}
}
Let me know if you need any more help.
Edit:
Here is the json file i used.
Code:
{"result":[{"ads": false}]}