-2

I am using Android Volley using https://foo.net/index.php?option=com_xxxxxxxadv using HTTPS. My code need two frequent requests e.g:

Request 1)

{
      "task": "login",
      "taskData": {
        "username": "donald",
        "type": "android",
        "devicetoken": "cinAz0hbctM:APA91bGmv9MQ2WNNGLxa2RJYJubmhL2",
        "long": "0",
        "password": "123456",
        "lat": "0"
      }
}

Request 2)

{
  "task": "profile",
  "taskData": {
    "username": "donald"
  }
}

it gives me the response of login(Request 1) perfectly but in Request 2 also it gives login(Request 1) response, when I checked it in Google Chrome Postman it gives a perfect response for both requests so there should be something to do on the Android side.

Now when I change URL to http://foo.com/index.php?option=com_xxxxxxxadv it works perfectly with the same code.

What I want is that it should give the exact same response which is request i.e request 1 response for request 1 and request 2 response for request 2.

halfer
  • 19,824
  • 17
  • 99
  • 186
Pratik Vyas
  • 644
  • 7
  • 20
  • @SachinBahukhandi: re your edit, words like HTTPS are just all-caps acronyms, and do not need code formatting. Similarly Postman is just a proper noun, so just an initial cap please, no bold. You missed Android and Volley, which are both proper nouns, and so should both get an initial cap. Use the code formatting tool just for code or I/O please, and go easy on the bold - thanks! – halfer May 01 '17 at 07:40
  • Pratik, please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer May 01 '17 at 07:40
  • 1
    thnx @halfer would keep it in mind from the next time. :) – Sachin Bahukhandi May 01 '17 at 07:49

3 Answers3

1

Data is not enough, it's depend how you call the API's through Volley tough see volley ssl support and how https query executed in android.

Community
  • 1
  • 1
Sam
  • 171
  • 1
  • 11
  • can you give me a simple example for it, the link you shared are good but I don't have any idea on how to generate and use Keystore (in BkS) – Pratik Vyas May 01 '17 at 11:12
  • there are examples included in links, please refer it well. @Pratik Vyas – Sam May 04 '17 at 13:12
0

create interface class in your package

public interface ApiResponseListener {

    void onSuccessResponse(String response, HashMap<String,String> hashMap);
    void onErrorResponse(VolleyError error, HashMap<String,String> hashMap);

}

create ApiController class in your package

public class ApiController {

    private Context context;
    public ApiResponseListener apiResponseListener;

    public ApiController(Context context) {
        this.context = context;

    }

    public void actionCallWebService(String url, final HashMap<String, String> params) {
//        
            StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            apiResponseListener.onSuccessResponse(response, params);

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            apiResponseListener.onErrorResponse(error, params);

                        }
                    }) {
                @Override
                protected Map<String, String> getParams() {
                    return params;
                }
            };
            stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                    Constants.MY_API_TIMEOUT_MS,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            MyApplication.getInstance().addToRequestQueue(stringRequest);

    }
}

in your fragment or activity class this two line

HashMap<String,String> params = new HasMap<>();
params.put("option","xyz");

apiController = new ApiController(this);
apiController.apiResponseListener = this;

apiController.actionCallWebService("htpps://www.xyz.com/index.php",params)

in your fragment or activity implement ApiResponseListener and overide below two method

 @Override
    public void onSuccessResponse(String response, HashMap<String, String> hashMap) {

        if (hashMap.get("option") != null && hashMap.get("option").equalsIgnoreCase("your option parameter value")) {

        }
        else if (hashMap.get("option") != null && hashMap.get("option").equalsIgnoreCase("your option parameter another value")) {

        }

    }

    @Override
    public void onErrorResponse(VolleyError error, HashMap<String, String> hashMap) {


    }

in your application class

public class MyApplication extends MultiDexApplication {

    public static final String TAG = MyApplication.class
            .getSimpleName();
    private RequestQueue queue;
    private static MyApplication mInstance;
    private Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        mInstance = this;

    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }


    public static synchronized MyApplication getInstance() {
        return mInstance;
    }

    public static synchronized com.android.volley.toolbox.ImageLoader getImageLoaderInstance() {
        return mImageLoader;
    }

    public RequestQueue getRequestQueue() {


        if (queue == null) {
            /*if(Common.httpclient==null)
            {
                Common.httpclient=new DefaultHttpClient();
                ClientConnectionManager mgr = Common.httpclient.getConnectionManager();

                HttpParams params = Common.httpclient.getParams();

                Common.httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(params,

                        mgr.getSchemeRegistry()), params);
                CookieStore cookieStore = new BasicCookieStore();
                Common.httpclient.setCookieStore( cookieStore );
            }
            HttpStack httpStack = new HttpClientStack( Common.httpclient );*/
            queue = Volley.newRequestQueue(getApplicationContext());
        }

        return queue;
    }



    public <T> void addToRequestQueue(com.android.volley.Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(com.android.volley.Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueueSuggestion(com.android.volley.Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueueSuggestion().add(req);
    }

    public <T> void addToRequestQueueSuggestion(com.android.volley.Request<T> req) {
        req.setTag(TAG);
        getRequestQueueSuggestion().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (queue != null) {
            queue.cancelAll(tag);
        }
    }

    public void cancelPendingRequestsSuggestion(Object tag) {
        if (queueSuggestion != null) {
            queueSuggestion.cancelAll(tag);
        }
    }

    public void stopQueue() {
        if (queue != null) {
            queue.stop();
        }
    }

}
Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13
0

I tried for this issue and finally got a solution, I used Handler to delay my request as the Https generally delays the response so it makes sense

Handler handler= new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //Your code after 5 seconds delay
                //Volley Request Code
            }
        },5000);
Pratik Vyas
  • 644
  • 7
  • 20