Volley第二次请求返回null

时间:2016-08-23 06:17:58

标签: android android-volley

第一个请求成功完成但队列中的第二个请求返回null,当设置断点并开始调试第二个请求时成功获取它的值

    class ListLoader extends AsyncTask<Void,Void,MerchantCategories[]>
{
    MerchantCategories[] data;
    protected void onPreExecute() {
        progressBar.setVisibility(View.VISIBLE);
    }
    @Override
    protected MerchantCategories[] doInBackground(Void... params) {
        Gson g = new Gson();
        gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
        try {
            regid = gcm.register(PROJECT_NUMBER);
            String msg="";
            msg = "Device registered, registration ID=" + regid;
            Log.i("GCM",  msg);
            EgxServices.getJsonFrom("http://inareg.com/APIs/RegisterAndroidDevice?registrationID="+regid,(Activity) c);
        } catch (IOException e) {
            e.printStackTrace();
        }

        MerchantCategories[] categs=g.fromJson(EgxServices.getJsonFrom("http://inareg.com/APIs/ListMerchantCategories",(Activity) c),MerchantCategories[].class);
        return categs;

    }

    protected void onPostExecute(MerchantCategories[] response) {
        if(response == null) {
            progressBar.setVisibility(View.GONE);

        }
        else{
        progressBar.setVisibility(View.GONE);
        MerchantCategoriesAdp adp =new MerchantCategoriesAdp(c,R.layout.lst_merchant_categories,response);
        drawerList.setAdapter(adp);
      //  Log.i("INFO", response);
      //  responseView.setText(response);
            }
    }
}

此方法用于初始化新请求并返回JSON字符串

    public static String getJsonFrom(final String urlStr, Activity context) {
    final Context c = context;
     final SharedValue value = new SharedValue();
     String result="";

    StringRequest request = new StringRequest(Request.Method.GET,urlStr,new Response.Listener<String>(){
        @Override
        public void onResponse(String response) {
            value.setResult(response);
        }

    },new  Response.ErrorListener(){

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(c, "No Internet Connection",
                    Toast.LENGTH_LONG).show();
        }
    });
    Volley.newRequestQueue(c).add(request);
    return value.getResult();
}

你可以看到第一个请求             EgxServices.getJsonFrom(“http://inareg.com/APIs/RegisterAndroidDevice?registrationID="+regid,(Activity) c);

成功运行,但第二个, EgxServices.getJsonFrom(“http://inareg.com/APIs/ListMerchantCategories",(Activity) c); 总是返回null值... 我需要知道为什么????? !!!

1 个答案:

答案 0 :(得分:0)

试试这个:

request.setShouldCache(false);

之前Volley.newRequestQueue(c).add(request);