how do i Remote control an app with json and volley?

ilovetobun69

Elite Member
Joined
Jun 23, 2017
Messages
1,526
Reaction score
328
how do i Remote control an app with json and volley?

I need to know so i can add a ad to my android app without google knowing . I want to be able to disable the ad .
 
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

i recommend you add this code in your
Application class
Code:
 AppConfig.getJSON("http://yoursite.com/ads.txt",this);
And now when you set your ads it's time to set ads
Code:
if (AppConfig.ENABLE_Ads){
           Toast.makeText(this, "Ads enabled", Toast.LENGTH_SHORT).show();
       } else {

           Toast.makeText(this, "Ads disable", Toast.LENGTH_SHORT).show();
       }
 
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

i recommend you add this code in your
Application class
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}]}
 
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}]}

wait all i had to do was that ? so i guess the json file i put "ads':true" to turn it on and off? thanks. ill let u know how it goes
 
If you put ads=true it will turn on
If you put ads=true it will turn on
getting messsage "cannot resolve ActivityPostDetails" and whats "collegeData.has" for? What would i switch this to? And
AppConfig.getJSON("http://yoursite.com/ads.txt",this); i put that wherever i want the ad?

if i change
Log.i(ActivityPostDetails.TAG,"response: "+ response); it says it cannot resolve the .tag too
 
getting messsage "cannot resolve ActivityPostDetails"
I created a Tag for Debug,
You can use String "Mytag" Like this
Code:
Log.i("Mytag","response: "+ response);
whats "collegeData.has" for
It's should be "ads", String from

Code:
JSONObject ads = result.getJSONObject(0);
AppConfig.getJSON("http://yoursite.com/ads.txt",this); i put that wherever i want the ad?
No put that in your Class where you
Code:
extends Application
more info here
Code:
https://stackoverflow.com/a/27416998/5806962
The
Code:
AppConfig.getJSON("http://yoursite.com/ads.txt",this);
code check if you enable the ads to not from your server and then it's change
public static boolean ENABLE_Ads = false: to that
Please let me know if you need any more help
 
Last edited:
Back
Top