Android Volley得到了改变的反应

时间:2017-05-01 06:48:20

标签: android https android-volley

我正在使用Android Volley https://foo.net/index.php?option=com_xxxxxxxadv使用HTTPS。我的代码需要两个频繁的请求,例如:

请求1)

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

请求2)

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

它完全给了我login(Request 1)的回复,但在请求2中也提供了login(Request 1)响应,当我在Google Chrome Postman中检查它时,它会对这两个请求给出完美的响应,所以应该有一些东西在Android方面做。

现在,当我将网址更改为http://foo.com/index.php?option=com_xxxxxxxadv时,它可以完全使用相同的代码。

我想要的是它应该给出完全相同的响应,即请求1请求1响应请求1和请求2响应请求2。

3 个答案:

答案 0 :(得分:1)

数据还不够,这取决于你如何通过Volley强调调用API,请参阅volley ssl supporthow https query executed in android

答案 1 :(得分:0)

在包中创建接口类

public interface ApiResponseListener {

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

}

在包中创建ApiController类

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);

    }
}

在您的片段或活动类中这两行

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)

在您的片段或活动中实现ApiResponseListener并覆盖以下两个方法

 @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) {


    }

在您的应用程序类

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();
        }
    }

}

答案 2 :(得分:0)

我尝试了这个问题并最终得到了一个解决方案,我使用Handler延迟我的请求,因为Https通常会延迟响应,所以它有意义

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