Home | History | Annotate | Download | only in volley
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.volley;
     18 
     19 /**
     20  * Encapsulates a parsed response for delivery.
     21  *
     22  * @param <T> Parsed type of this response
     23  */
     24 public class Response<T> {
     25 
     26     /** Callback interface for delivering parsed responses. */
     27     public interface Listener<T> {
     28         /** Called when a response is received. */
     29         public void onResponse(T response);
     30     }
     31 
     32     /** Callback interface for delivering error responses. */
     33     public interface ErrorListener {
     34         /**
     35          * Callback method that an error has been occurred with the
     36          * provided error code and optional user-readable message.
     37          */
     38         public void onErrorResponse(VolleyError error);
     39     }
     40 
     41     /** Returns a successful response containing the parsed result. */
     42     public static <T> Response<T> success(T result, Cache.Entry cacheEntry) {
     43         return new Response<T>(result, cacheEntry);
     44     }
     45 
     46     /**
     47      * Returns a failed response containing the given error code and an optional
     48      * localized message displayed to the user.
     49      */
     50     public static <T> Response<T> error(VolleyError error) {
     51         return new Response<T>(error);
     52     }
     53 
     54     /** Parsed response, or null in the case of error. */
     55     public final T result;
     56 
     57     /** Cache metadata for this response, or null in the case of error. */
     58     public final Cache.Entry cacheEntry;
     59 
     60     /** Detailed error information if <code>errorCode != OK</code>. */
     61     public final VolleyError error;
     62 
     63     /** True if this response was a soft-expired one and a second one MAY be coming. */
     64     public boolean intermediate = false;
     65 
     66     /**
     67      * Returns whether this response is considered successful.
     68      */
     69     public boolean isSuccess() {
     70         return error == null;
     71     }
     72 
     73 
     74     private Response(T result, Cache.Entry cacheEntry) {
     75         this.result = result;
     76         this.cacheEntry = cacheEntry;
     77         this.error = null;
     78     }
     79 
     80     private Response(VolleyError error) {
     81         this.result = null;
     82         this.cacheEntry = null;
     83         this.error = error;
     84     }
     85 }
     86