【问题标题】:Android Volley getting altered responseAndroid Volley 得到改变的响应
【发布时间】:2017-09-28 15:33:10
【问题描述】:

我正在使用 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 邮递员中检查它时,它对这两个请求都给出了完美的响应,所以应该有一些事情要做安卓端。

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

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

【问题讨论】:

  • @SachinBahukhandi:重新编辑,像 HTTPS 这样的词只是全大写的首字母缩写词,不需要代码格式。同样,Postman 只是一个专有名词,所以请只是一个首字母大写,不要粗体。您错过了 Android 和 Volley,它们都是专有名词,因此都应该有一个初始上限。请仅对代码或 I/O 使用代码格式化工具,并在粗体上放轻松 - 谢谢!
  • Pratik,请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
  • thnx @halfer 下次会记住这一点。 :)

标签: android https android-volley


【解决方案1】:

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

【讨论】:

  • 你能给我一个简单的例子吗,你分享的链接很好,但我不知道如何生成和使用密钥库(在 BkS 中)
  • 链接中有例子,请参考。 @Pratik Vyas
【解决方案2】:

在你的包中创建接口类

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

}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-11-18
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多