| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 package org.chromium.chrome.browser.physicalweb; | 4 package org.chromium.chrome.browser.physicalweb; |
| 5 | 5 |
| 6 import org.json.JSONException; | 6 import org.json.JSONException; |
| 7 import org.json.JSONObject; | 7 import org.json.JSONObject; |
| 8 | 8 |
| 9 import java.io.ByteArrayOutputStream; | 9 import java.io.ByteArrayOutputStream; |
| 10 import java.io.IOException; | 10 import java.io.IOException; |
| 11 import java.io.InputStream; | 11 import java.io.InputStream; |
| 12 import java.io.OutputStream; | 12 import java.io.OutputStream; |
| 13 import java.net.HttpURLConnection; | 13 import java.net.HttpURLConnection; |
| 14 import java.net.MalformedURLException; | 14 import java.net.MalformedURLException; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * A class that represents an HTTP request for a JSON object. | 17 * A class that represents an HTTP request for a JSON object. |
| 18 * Both the request payload and the response are JSON objects. | 18 * Both the request payload and the response are JSON objects. |
| 19 */ | 19 */ |
| 20 class JsonObjectHttpRequest extends HttpRequest<JSONObject> { | 20 class JsonObjectHttpRequest extends HttpRequest<JSONObject> { |
| 21 private final JSONObject mJsonObject; | 21 private final JSONObject mJsonObject; |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * Construct a JSON object request. | 24 * Construct a JSON object request. |
| 25 * @param url The url to make this HTTP request to. | 25 * @param url The url to make this HTTP request to. |
| 26 * @param userAgent The string to set as the User-Agent request header. |
| 27 * @param acceptLanguage The string to set as the Accept-Language request he
ader. |
| 26 * @param jsonObject The JSON payload. | 28 * @param jsonObject The JSON payload. |
| 27 * @param callback The callback run when the HTTP response is received. | 29 * @param callback The callback run when the HTTP response is received. |
| 28 * @throws MalformedURLException on invalid url | 30 * @throws MalformedURLException on invalid url |
| 29 */ | 31 */ |
| 30 public JsonObjectHttpRequest(String url, JSONObject jsonObject, RequestCallb
ack callback) | 32 public JsonObjectHttpRequest(String url, String userAgent, String acceptLang
uage, |
| 33 JSONObject jsonObject, RequestCallback callback) |
| 31 throws MalformedURLException { | 34 throws MalformedURLException { |
| 32 super(url, callback); | 35 super(url, userAgent, acceptLanguage, callback); |
| 33 mJsonObject = jsonObject; | 36 mJsonObject = jsonObject; |
| 34 } | 37 } |
| 35 | 38 |
| 36 /** | 39 /** |
| 37 * The callback that gets run after the request is made. | 40 * The callback that gets run after the request is made. |
| 38 */ | 41 */ |
| 39 public interface RequestCallback extends HttpRequest.HttpRequestCallback<JSO
NObject> {} | 42 public interface RequestCallback extends HttpRequest.HttpRequestCallback<JSO
NObject> {} |
| 40 | 43 |
| 41 /** | 44 /** |
| 42 * Helper method to make an HTTP request. | 45 * Helper method to make an HTTP request. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 70 private static String readStreamToString(InputStream is) throws IOException
{ | 73 private static String readStreamToString(InputStream is) throws IOException
{ |
| 71 ByteArrayOutputStream os = new ByteArrayOutputStream(); | 74 ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 72 byte[] buffer = new byte[1024]; | 75 byte[] buffer = new byte[1024]; |
| 73 int len; | 76 int len; |
| 74 while ((len = is.read(buffer)) != -1) { | 77 while ((len = is.read(buffer)) != -1) { |
| 75 os.write(buffer, 0, len); | 78 os.write(buffer, 0, len); |
| 76 } | 79 } |
| 77 return os.toString("UTF-8"); | 80 return os.toString("UTF-8"); |
| 78 } | 81 } |
| 79 } | 82 } |
| OLD | NEW |