回答

0

使用凌空解析您的數據可以這樣實現:

  1. 創建類VolleyService 公共類VolleyService {

    private static VolleyService instance; 
    private RequestQueue requestQueue; 
    private ImageLoader imageLoader; 
    
    private VolleyService(Context context) { 
        requestQueue = Volley.newRequestQueue(context); 
    
        imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() { 
         private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20); 
    
         @Override 
         public Bitmap getBitmap(String url) { 
          return cache.get(url); 
         } 
    
         @Override 
         public void putBitmap(String url, Bitmap bitmap) { 
          cache.put(url,bitmap); 
         } 
        }); 
    } 
    
    public static VolleyService getInstance(Context context) { 
        if (instance == null) { 
         instance = new VolleyService(context); 
        } 
        return instance; 
    } 
    
    public RequestQueue getRequestQueue() { 
        return requestQueue; 
    } 
    
    public ImageLoader getImageLoader() { 
        return imageLoader; 
    } 
    

    }

  2. 使用方法如下這用於創建請求並獲取URL的JSON響應:

    請求隊列隊列=

    VolleyService.getInstance(this.getContext()).getRequestQueue();  
        StringRequest request = new StringRequest(url, new 
        Response.Listener<String>() { 
         @Override 
         public void onResponse(String response) { 
          // we got the response, now our job is to handle it 
          try { 
           updateArticleData(response, syncResult,categoryID); 
          } catch (RemoteException | OperationApplicationException e) { 
           e.printStackTrace(); 
          } 
         } 
        }, new Response.ErrorListener() { 
    
         @Override 
         public void onErrorResponse(VolleyError error) { 
          //something happened, treat the error. 
          Log.e("Error", error.toString()); 
         } 
        }); 
    
        queue.add(request); 
    
  3. Mehod getJsonFromResponse(響應):

    private SomeClass getDataFromJson(String json) {  
        //Do sth with json  
        return someData; 
    }