Okhttp3 source parsing (2) -Request analysis

### Introduction Earlier we talked about [Okhttp basic usage] (https://www.jianshu.com/p/8e404d9c160f) [Okhttp3 source parsing (1) -OkHttpClient analysis] (https://www.jianshu.com / p / bf1d01b79ce7) analysis Request major source today! ### Request Initialization When we construct OkHttpClient finished objects need to construct the Request object, is configured as follows: ###### 1.Get request `` `final Request request = new Request.Builder () .url (" https: //www.wanandroid.com/navi/json ") .get () .build ();` `` ###### 2.POST POST request to take the request to submit the form, then you need to declare an object of a RequestBody `` `requestBody requestBody = new FormBody.Builder () .add (" username "," qinzishuai ") .add (" password "," 123456 ") .build (); Request request = new Request.Builder () .url ( "https://www.wanandroid.com/user/login") .post (requestBody) .build (); `` `see the above code is not very familiar with? And OkHttpClient very similar, but also to build the Request Builder mode Yes! ! [] (Https://img2018.cnblogs.com/blog/1312938/201908/1312938-20190823180241877-899976016. png) we click Request source code into it, which really Builder static inner classes:! [] (https://img2018.cnblogs.com/blog/1312938/201908/1312938-20190823180242273-1916386988.png) Then we check * * Request during initial configuration which parameters? ? ? ** `` `public static class Builder {HttpUrl url; String method; Headers.Builder headers; RequestBody body; public Builder () {this.method =" GET "; this.headers = new Headers.Builder ();} / / omit part of the code public Request build () {if (url == null) throw new IllegalStateException ( "url == null"); return new Request (this);}} `` `seen from the code, if not specified, the default Get request is `this.method =" GET "`, `url` fields such as for our own need to configure: ###### HttpUrl requesting access url, String and can pass a specific URL as follows:` `` public Builder url (String url) {if (url == null) throw new NullPointerException ( "url == null"); // Silently replace web socket URLs with HTTP URLs if (url.regionMatches (true, 0, "ws.: ", 0, com / blog / 1312938/201908 / 1312938-20190823180242513-433063641.png) If a POST request, we need to set up! [] (https://img2018.cnblogs.com/blog/1312938/201908/1312938- 20190823180242695-600270897.png) ### requestBody resolve first, we look at how requestBody initialization? ? Take for example the form is submitted: `` `RequestBody requestBody = new FormBody.Builder () .add (" username "," qinzishuai ") .add (" password "," 000000 ") .build ();` `` expected also Builder mode, and `RequestBody` is an abstract class,` FormBody` `RequestBody` which is an implementation class, another implementation class is` MultipartBody` requestBody source follows: `` `public abstract class requestBody {/ ** . Returns the Content-Type header for this body * / public abstract @Nullable MediaType contentType (); / ** * Returns the number of bytes that will be written to {@code sink} in a call to {@link #writeTo} ., * or -1 if that count is unknown * / public long contentLength () throws IOException {return -1; final ByteString content) {return new RequestBody () {@Override public @Nullable MediaType contentType () {return contentType;} @Override public long contentLength () throws IOException {return content.size ();} @Override public void writeTo (BufferedSink sink) throws IOException {sink.write (content);}};.} / ** Returns a new request body that transmits {@code content} * / public static requestBody create (final @Nullable MediaType contentType, final byte [] content ) {return create (contentType, content, 0, content.length);} // ...} omit part of the code `core three methods: - contentType () // data type - contentLength () // data length - writeTo (BufferedSink sink) // write today will stop here, we want to help ... we can focus my micro-channel public number: "Qin Shuai child" a quality, public attitudes numbers! ! [Public number] (https://img2018.cnblogs.com/blog/1312938/201908/1312938-20190823180242882-2108811045.jpg) } @Override public long contentLength () throws IOException {return content.size ();} @Override public void writeTo (BufferedSink sink) throws IOException {sink.write (content);}};} / ** Returns a new request body that transmits {@code content} * / public static requestBody create (final @Nullable MediaType contentType, final byte [] content) {return create (contentType, content, 0, content.length);}. // omit part of the code .. .} `` there are three core methods: - contentType () // data types - contentLength () // data length - writeTo (BufferedSink sink) // write operation will stop here today, we want to help .. everyone can focus on my micro-channel public number: "Qin Shuai child" a quality, public attitudes numbers! ! [Public number] (https://img2018.cnblogs.com/blog/1312938/201908/1312938-20190823180242882-2108811045.jpg) } @Override public long contentLength () throws IOException {return content.size ();} @Override public void writeTo (BufferedSink sink) throws IOException {sink.write (content);}};} / ** Returns a new request body that transmits {@code content} * / public static requestBody create (final @Nullable MediaType contentType, final byte [] content) {return create (contentType, content, 0, content.length);}. // omit part of the code .. .} `` there are three core methods: - contentType () // data types - contentLength () // data length - writeTo (BufferedSink sink) // write operation will stop here today, we want to help .. everyone can focus on my micro-channel public number: "Qin Shuai child" a quality, public attitudes numbers! ! [Public number] (https://img2018.cnblogs.com/blog/1312938/201908/1312938-20190823180242882-2108811045.jpg) } / ** Returns a new request body that transmits {@code content}. * / Public static RequestBody create (final @Nullable MediaType contentType, final byte [] content) {return create (contentType, content, 0, content.length) ;}} // omit part of the code ... `core three methods: - contentType () // data type - contentLength () // data length - writeTo (BufferedSink sink) // write stop here today , we hope to help ... we can focus my micro-channel public number: "Qin Shuai child" a quality, public attitudes numbers! ! [Public number] (https://img2018.cnblogs.com/blog/1312938/201908/1312938-20190823180242882-2108811045.jpg) } / ** Returns a new request body that transmits {@code content}. * / Public static RequestBody create (final @Nullable MediaType contentType, final byte [] content) {return create (contentType, content, 0, content.length) ;}} // omit part of the code ... `core three methods: - contentType () // data type - contentLength () // data length - writeTo (BufferedSink sink) // write stop here today , we hope to help ... we can focus my micro-channel public number: "Qin Shuai child" a quality, public attitudes numbers! ! [Public number] (https://img2018.cnblogs.com/blog/1312938/201908/1312938-20190823180242882-2108811045.jpg)

Guess you like

Origin www.cnblogs.com/qinzishuai/p/11401790.html