Commit 8c3afbcd authored by wanddy's avatar wanddy Committed by Evan W. Patton

Implement ResponseTextEncoding property for Web component

Change-Id: I62e4acf4abec9e71f5d2804db9f381c5a2877bae
parent 760770dc
......@@ -640,6 +640,7 @@ ReverseDirectionProperties = 方向倒转
SensorValueChangedEventEnabledProperties = 启用传感器值改变事件
TachoCountChangedEventEnabledProperties = 启用角度改变事件
UnitProperties = 单位
ResponseTextEncodingProperties = 响应内容字符编码
#Parameters
xAccelParams = X分量
......
......@@ -1882,6 +1882,11 @@ public final class YoungAndroidFormUpgrader {
// No properties need to be modified to upgrade to version 8.
srcCompVersion = 8;
}
if (srcCompVersion < 9) {
// The ResponseTextEncoding property was added.
// Properties related to this component have now been upgraded to version 9
srcCompVersion = 9;
}
return srcCompVersion;
}
......
......@@ -3386,7 +3386,10 @@ Blockly.Versioning.AllUpgradeMaps =
7: "noUpgrade",
// AI2: Added methods PatchText, PatchTextWithEncoding, and PatchFile
8: "noUpgrade"
8: "noUpgrade",
// AI2: Added ResponseTextEncoding property
9: "noUpgrade"
}, // End Web upgraders
......
......@@ -583,8 +583,9 @@ public class YaVersion {
// - REGRESSION_COMPONENT_VERSION was introduced
// - TEXTTOSPEEECH_COMPONENT_VERSION was incremented to 6
// - TINYDB_COMPONENT_VERSION was incremented to 3
public static final int YOUNG_ANDROID_VERSION = 224;
// For YOUNG_ANDROID_VERSION 225:
// - WEB_COMPONENT_VERSION was incremented to 9
public static final int YOUNG_ANDROID_VERSION = 225;
// ............................... Blocks Language Version Number ...............................
......@@ -1534,7 +1535,9 @@ public class YaVersion {
// - The XMLTextDecodeAsDictionary method was added.
// For WEB_COMPONENT_VERSION 8:
// - PATCH methods added (PatchText, PatchTextWithEncoding, and PatchFile).
public static final int WEB_COMPONENT_VERSION = 8;
// For WEB_COMPONENT_VERSION 9:
// - Added property ResponseTextEncoding
public static final int WEB_COMPONENT_VERSION = 9;
// For WEBVIEWER_COMPONENT_VERSION 2:
// - The CanGoForward and CanGoBack methods were added
......
......@@ -241,6 +241,7 @@ public class Web extends AndroidNonvisibleComponent implements Component,
// Set of observers
private HashSet<DataSourceChangeListener> dataSourceObservers = new HashSet<>();
private String responseTextEncoding = "UTF-8";
/**
* Creates a new Web component.
......@@ -286,6 +287,25 @@ public class Web extends AndroidNonvisibleComponent implements Component,
urlString = url;
}
/**
* Returns the Response Text Encoding.
*/
@SimpleProperty(category = PropertyCategory.BEHAVIOR,
description = "User-specified character encoding for web response.")
public String ResponseTextEncoding() {
return responseTextEncoding;
}
/**
* Specifies the Response Text Encoding.
*/
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_STRING,
defaultValue = "UTF-8")
@SimpleProperty
public void ResponseTextEncoding(String encoding) {
responseTextEncoding = encoding;
}
/**
* The request headers, as a list of two-element sublists. The first element of each sublist
* represents the request header field name. The second element of each sublist represents the
......@@ -1241,7 +1261,7 @@ public class Web extends AndroidNonvisibleComponent implements Component,
}
});
} else {
final String responseContent = getResponseContent(connection);
final String responseContent = getResponseContent(connection, responseTextEncoding);
// Dispatch the event.
activity.runOnUiThread(new Runnable() {
......@@ -1431,11 +1451,15 @@ public class Web extends AndroidNonvisibleComponent implements Component,
}
}
private static String getResponseContent(HttpURLConnection connection) throws IOException {
private static String getResponseContent(HttpURLConnection connection, String encodingProperty) throws IOException {
// Use the content encoding to convert bytes to characters.
String encoding = connection.getContentEncoding();
if (encoding == null) {
encoding = "UTF-8";
if (encodingProperty == null || encodingProperty.isEmpty()) {
encoding = "UTF-8";
} else {
encoding = encodingProperty;
}
}
InputStreamReader reader = new InputStreamReader(getConnectionStream(connection), encoding);
try {
......
......@@ -531,6 +531,8 @@ set the following properties:
<dd>Specifies the name of the file where the response should be saved.
If SaveResponse is true and ResponseFileName is empty, then a new file
name will be generated.</dd>
<dt id="Web.ResponseTextEncoding" class="text"><em>ResponseTextEncoding</em></dt>
<dd>Specifies the Response Text Encoding.</dd>
<dt id="Web.SaveResponse" class="boolean"><em>SaveResponse</em></dt>
<dd>Specifies whether the response should be saved in a file.</dd>
<dt id="Web.Timeout" class="number"><em>Timeout</em></dt>
......
......@@ -471,6 +471,9 @@ Non-visible component that provides functions for HTTP GET, POST, PUT, and DELET
If SaveResponse is true and ResponseFileName is empty, then a new file
name will be generated.
{:id="Web.ResponseTextEncoding" .text} *ResponseTextEncoding*
: Specifies the Response Text Encoding.
{:id="Web.SaveResponse" .boolean} *SaveResponse*
: Specifies whether the response should be saved in a file.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment