2016-08-23 117 views
0

的第一個請求成功完成,但設置斷點並開始調試第二請求時在隊列返回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); 總是返回空值... 我需要知道爲什麼????? !!!

回答

0

試試這個:

request.setShouldCache(false); 

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

+0

我試過,但相同的結果:( –

+0

我也注意到,它僅在調試返回值,如果我通過響應結果到Log.v( 「SD」, 「排球響應+++」 +響應);內onResponse –

+0

該問題可能是從方式傳遞導致 –