0

I'm using one Generic Java Class named GetVolleyResponse to send and receive data to server using volley. I want to show progressDialog from the same class to All other possible calling Activities.I've tried something but it is not working. GetVolleyResponse.java

public class GetVolleyResponse {
    ProgressDialog progress;
    Context ctx;
    AlertDialog.Builder alert;
    GetVolleyResponse(Context ctx)
    {
        this.ctx=ctx;
        progress=new ProgressDialog(this.ctx);
        progress.setTitle("please wait...");

    }
    public void getResponse(String url, final Map params, final VolleyCallback callback)
    {
         progress.show();
        StringRequest stringRequest=new StringRequest( Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progress.dismiss();
             callback.onSuccessResponse(response);//Interface for callback Defined by me..
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progress.dismiss();
                String message = null;
                if (error instanceof NetworkError) {
                    message = "Cannot connect to Internet...Please check your connection!";
                } else if (error instanceof ServerError) {
                    message = "The server could not be found. Please try again after some time!!";
                } else if (error instanceof AuthFailureError) {
                    message = "Cannot connect to Internet...Please check your connection!";
                } else if (error instanceof ParseError) {
                    message = "Parsing error! Please try again after some time!!";
                } else if (error instanceof NoConnectionError) {
                    message = "Cannot connect to Internet...Please check your connection!";
                } else if (error instanceof TimeoutError) {
                    message = "Connection TimeOut! Please check your internet connection.";
                }
               callback.onErrorResponse(message);
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
        };
        MySingleton.getInstance(ctx).addToRequestQueue(stringRequest);
    }
}

but because of progressDialog app is crashing..

error Log:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.accer.sportsgr, PID: 25143
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.accer.sportsgr/com.example.accer.sportsgr.Updater}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
                      at android.view.ViewRootImpl.setView(ViewRootImpl.java:682)
                      at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
                      at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                      at android.app.Dialog.show(Dialog.java:316)
                      at com.example.accer.sportsgr.GetVolleyResponse.getResponse(GetVolleyResponse.java:41)
                      at com.example.accer.sportsgr.Updater.onCreate(Updater.java:70)
                      at android.app.Activity.performCreate(Activity.java:6662)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Here is the call from updater.java

 (new GetVolleyResponse(getApplicationContext())).getResponse(Register.serverUrl + UploadUrl, params, new VolleyCallback() {
                @Override
                public void onSuccessResponse(String result) {
                    try {
                        JSONArray jsonArray=new JSONArray(result);
                        JSONObject jsonObject;
                        for (int i=0;i<jsonArray.length();i++)
                        {
                            jsonObject=jsonArray.getJSONObject(i);
                            sp_list.add(jsonObject.getString("name"));
                        }
                        sp_list.add("others");
                        setSpinner();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onErrorResponse(String message) {
                   Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();
                }
            });

how should i call the above constructor from any actvity

mahendra
  • 367
  • 5
  • 18

1 Answers1

0
(new GetVolleyResponse(this)).getResponse(Register.serverUrl + UploadUrl, params, new VolleyCallback() {
            @Override
            public void onSuccessResponse(String result) {
                try {
                    JSONArray jsonArray=new JSONArray(result);
                    JSONObject jsonObject;
                    for (int i=0;i<jsonArray.length();i++)
                    {
                        jsonObject=jsonArray.getJSONObject(i);
                        sp_list.add(jsonObject.getString("name"));
                    }
                    sp_list.add("others");
                    setSpinner();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(String message) {
               Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();
            }
        });

Use this or youractivity.this as your context parameter instead.Progress Dialog couldn't recognize getApplicationContext() as context I don't know why but if you try to pass create progressDialog with getAplicationContext() as context it will crash.

From Fragment use getActivity. The way you call your class is correct.

teck wei
  • 1,375
  • 11
  • 22
  • thanq. its working fine. what is the difference between context and this? – mahendra Dec 22 '16 at 09:32
  • Basically this can be context or activity itself. So there is no difference between context and this just I don't know why while create progressDialog by using getApplicationContext() context it cannot work. For difference between context and activity look at this link http://stackoverflow.com/questions/6518206/what-is-the-difference-between-activity-and-context – teck wei Dec 22 '16 at 09:34