/** * Begin a load with Glide by passing in a context. * * <p> Any requests started using a context will only have theapplication level options applied * and will not be started or stopped based on lifecycle events. In general, loads should be * started atthe level theresult will be used in. If the resource will be used in a view in a * child fragment, the load should be started with {@link #with(android.app.Fragment)}} using that * child fragment. Similarly, ifthe resource will be used in a view inthe parent fragment, the * load should be started with {@link #with(android.app.Fragment)} using the parent fragment. In * the same vein, ifthe resource will be used in a view in an activity, the load should be * started with {@link #with(android.app.Activity)}}. </p> * * <p> This method is appropriate for resources that will be used outside ofthe normal fragment * or activity lifecycle (For example in services, orfor notification thumbnails). </p> * * @param context Any context, will not be retained. * @return A RequestManager forthe top level applicationthat can be used to start a load. * @see #with(android.app.Activity) * @see #with(android.app.Fragment) * @see #with(android.support.v4.app.Fragment) * @see #with(android.support.v4.app.FragmentActivity) */ public static RequestManager with(Context context) { RequestManagerRetriever retriever = RequestManagerRetriever.get(); return retriever.get(context); }
/** * Begin a load with Glide that will be tied tothegiven {@link android.app.Activity}'s lifecycle * andthat uses thegiven {@link Activity}'s default options. * * @param activity The activity to use. * @return A RequestManager forthegiven activity that can be used to start a load. */ public static RequestManager with(Activity activity) { RequestManagerRetriever retriever = RequestManagerRetriever.get(); return retriever.get(activity); }
/** * Begin a load with Glide that will tied tothe give * {@link android.support.v4.app.FragmentActivity}'s lifecycle andthat uses thegiven * {@link android.support.v4.app.FragmentActivity}'s default options. * * @param activity The activity to use. * @return A RequestManager forthegiven FragmentActivity that can be used to start a load. */ public static RequestManager with(FragmentActivity activity) { RequestManagerRetriever retriever = RequestManagerRetriever.get(); return retriever.get(activity); }
/** * Begin a load with Glide that will be tied tothegiven {@link android.app.Fragment}'s lifecycle * andthat uses thegiven {@link android.app.Fragment}'s default options. * * @param fragment The fragment to use. * @return A RequestManager forthegiven Fragment that can be used to start a load. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) public static RequestManager with(android.app.Fragment fragment) { RequestManagerRetriever retriever = RequestManagerRetriever.get(); return retriever.get(fragment); }
/** * Begin a load with Glide that will be tied tothegiven * {@link android.support.v4.app.Fragment}'s lifecycle andthat uses thegiven * {@link android.support.v4.app.Fragment}'s default options. * * @param fragment The fragment to use. * @return A RequestManager forthegiven Fragment that can be used to start a load. */ public static RequestManager with(Fragment fragment) { RequestManagerRetriever retriever = RequestManagerRetriever.get(); return retriever.get(fragment); }
* A view-less {@link android.support.v4.app.Fragment} used to safely store an {@link * com.bumptech.glide.RequestManager} that can be used to start, stop and manage Glide requests * started for targets within the fragment or activity this fragment is a child of. * public classSupportRequestManagerFragmentextendsFragment
HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth. OkHttp is an HTTP client that’s efficient by default:
HTTP/2 support allows all requests to the same host to share a socket. Connection pooling reduces request latency (if HTTP/2 isn’t available). Transparent GZIP shrinks download sizes. Response caching avoids the network completely for repeat requests. OkHttp perseveres when the network is troublesome: it will silently recover from common connection problems. If your service has multiple IP addresses OkHttp will attempt alternate addresses if the first connect fails. This is necessary for IPv4+IPv6 and for services hosted in redundant data centers. OkHttp initiates new connections with modern TLS features (SNI, ALPN), and falls back to TLS 1.0 if the handshake fails.
Using OkHttp is easy. Its request/response API is designed with fluent builders and immutability. It supports both synchronous blocking calls and async calls with callbacks.
OkHttp supports Android 2.3 and above. For Java, the minimum requirement is 1.7.
/** * A callis a request that has been prepared for execution. A call can be * canceled. As this object represents a single request/response pair (stream), * it cannot be executed twice. */
protected Call(OkHttpClient client, Request originalRequest) { // Copy the client. Otherwise changes (socket factory, redirect policy, // etc.) may incorrectly be reflected in the request when it is executed. this.client = client.copyWithDefaults(); this.originalRequest = originalRequest; }
/** * Performs the request and returns the response. May return null if this * call was canceled. */ Response getResponse(Request request, boolean forWebSocket) throws IOException { // Copy body metadata to the appropriate request headers. RequestBody body = request.body(); if(body != null) { Request.Builder requestBuilder = request.newBuilder();
// Create the initial HTTP engine. Retries and redirects need new engine for each attempt. engine = new HttpEngine(client, request, false, false, forWebSocket, null, null, null, null);
int followUpCount = 0; while (true) { if(canceled) { engine.releaseConnection(); throw new IOException("Canceled"); }
try { engine.sendRequest(); engine.readResponse(); } catch (RequestException e) { // The attempt to interpret the request failed. Give up. throw e.getCause(); } catch (RouteException e) { // The attempt to connect via a route failed. The request will not have been sent. HttpEngine retryEngine = engine.recover(e); if(retryEngine != null) { engine = retryEngine; continue; } // Give up; recovery is not possible. throw e.getLastConnectException(); } catch (IOException e) { // An attempt to communicate with a server failed. The request may have been sent. HttpEngine retryEngine = engine.recover(e, null); if(retryEngine != null) { engine = retryEngine; continue; }
* Executes the given task sometime in the future. The task
* may execute in a new thread or in an existing pooled thread.
*
* If the task cannot be submitted for execution, either because this
* executor has been shutdown or because its capacity has been reached,
* the task is handled by the current {@code RejectedExecutionHandler}.
public void execute(Runnable command) {
}
public static final String BASE_URL = "http://api.myservice.com/"; Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build();
定义一个Retrofit接口一共需要指定两样东西。 (1)指定HTTP的动作 我们可以选择的参数有四个,分别为: GET POST PUT DELETE 不清楚这几个动作在HTTP中的具体的含义的同学可以自行百度再仔细研究。 这里我推荐一篇文章: http://286.iteye.com/blog/1420713 在后面的参数为BASE_URL后另加入的地址,从而指定我们要调用的Api的URL。 这里有一个坑! BaseURL和@GET后的附加的URL的问题。 我使用的retrofit2.0在BaseURL后的URL必须以/结尾!! 也就是说