OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net.urlconnection; | 5 package org.chromium.net.urlconnection; |
6 | 6 |
7 import android.util.Pair; | 7 import android.util.Pair; |
8 | 8 |
9 import org.chromium.net.ExtendedResponseInfo; | 9 import org.chromium.net.ExtendedResponseInfo; |
10 import org.chromium.net.ResponseInfo; | 10 import org.chromium.net.ResponseInfo; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 /** | 107 /** |
108 * Returns the response code returned by the remote HTTP server. | 108 * Returns the response code returned by the remote HTTP server. |
109 */ | 109 */ |
110 @Override | 110 @Override |
111 public int getResponseCode() throws IOException { | 111 public int getResponseCode() throws IOException { |
112 connect(); | 112 connect(); |
113 return mResponseInfo.getHttpStatusCode(); | 113 return mResponseInfo.getHttpStatusCode(); |
114 } | 114 } |
115 | 115 |
116 /** | 116 /** |
117 * Returns an unmodifiable map of the response-header fields and values. | |
118 */ | |
119 @Override | |
120 public Map<String, List<String>> getHeaderFields() { | |
121 try { | |
122 connect(); | |
123 } catch (IOException e) { | |
124 // Default implementation returns an empty map on K, but returns | |
125 // null on L. | |
126 return null; | |
miloslav
2015/01/20 15:25:10
Do we really want to return null? That doesn't see
xunjieli
2015/01/20 16:12:21
Sorry about the confusion. I was mistaken. On k it
| |
127 } | |
128 return mResponseInfo.getAllHeaders(); | |
129 } | |
130 | |
131 /** | |
132 * Returns the value of the named header field. If called on a connection | |
133 * that sets the same header multiple times with possibly different values, | |
134 * only the last value is returned. | |
135 */ | |
136 @Override | |
137 public final String getHeaderField(String fieldName) { | |
138 try { | |
139 connect(); | |
140 } catch (IOException e) { | |
141 return null; | |
142 } | |
143 Map<String, List<String>> map = mResponseInfo.getAllHeaders(); | |
144 if (!map.containsKey(fieldName)) { | |
145 return null; | |
146 } | |
147 List<String> values = map.get(fieldName); | |
148 return values.get(values.size() - 1); | |
149 } | |
150 | |
151 /** | |
152 * Returns the name of the header field at the given position pos, or null | |
153 * if there are fewer than pos fields. | |
154 */ | |
155 @Override | |
156 public final String getHeaderFieldKey(int pos) { | |
157 try { | |
158 connect(); | |
159 } catch (IOException e) { | |
160 return null; | |
161 } | |
162 List<Pair<String, String>> headers = | |
163 mResponseInfo.getAllHeadersAsList(); | |
164 if (pos >= headers.size()) { | |
165 return null; | |
166 } | |
167 return headers.get(pos).first; | |
168 } | |
169 | |
170 /** | |
171 * Returns the header value at the field position pos or null if the header | |
172 * has fewer than pos fields. | |
173 */ | |
174 @Override | |
175 public final String getHeaderField(int pos) { | |
176 try { | |
177 connect(); | |
178 } catch (IOException e) { | |
179 return null; | |
180 } | |
181 List<Pair<String, String>> headers = | |
182 mResponseInfo.getAllHeadersAsList(); | |
183 if (pos >= headers.size()) { | |
184 return null; | |
185 } | |
186 return headers.get(pos).second; | |
187 } | |
188 | |
189 /** | |
117 * Returns an InputStream for reading data from the resource pointed by this | 190 * Returns an InputStream for reading data from the resource pointed by this |
118 * URLConnection. | 191 * URLConnection. |
119 * @throws FileNotFoundException if http response code is equal or greater | 192 * @throws FileNotFoundException if http response code is equal or greater |
120 * than {@link HTTP_BAD_REQUEST}. | 193 * than {@link HTTP_BAD_REQUEST}. |
121 * @throws IOException If the request gets a network error or HTTP error | 194 * @throws IOException If the request gets a network error or HTTP error |
122 * status code, or if the caller tried to read the response body | 195 * status code, or if the caller tried to read the response body |
123 * of a redirect when redirects are disabled. | 196 * of a redirect when redirects are disabled. |
124 */ | 197 */ |
125 @Override | 198 @Override |
126 public InputStream getInputStream() throws IOException { | 199 public InputStream getInputStream() throws IOException { |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
338 */ | 411 */ |
339 private void checkHasResponse() throws IOException { | 412 private void checkHasResponse() throws IOException { |
340 if (mException != null) { | 413 if (mException != null) { |
341 throw mException; | 414 throw mException; |
342 } else if (mResponseInfo == null) { | 415 } else if (mResponseInfo == null) { |
343 throw new NullPointerException( | 416 throw new NullPointerException( |
344 "Response info is null when there is no exception."); | 417 "Response info is null when there is no exception."); |
345 } | 418 } |
346 } | 419 } |
347 } | 420 } |
OLD | NEW |