Cannot retrieve text/plain or simple application/json content via makeWebRequest
o The Environment:
Windows 10
Eclipse Neon.2 (4.6.2)
ConnectIQ 2.2.3
o A detailed description of the issue
Some web services return text/plain or simple application/json responses, but these are not handled properly by makeWebRequest.
B]o Steps to reproduce the issue[/B]
Run the python server code, then build and run the test case below. You will need to click the simulated device screen or the start button to trigger each web request. The screen will show the name of the file that is being retrieved, and then the status when it completes.
o Any applicable additional information
The application will try to retrieve legal text/plain and application/json data. The framework will properly handle json array and object data, but it does not handle any text/plain responses or application/json responses of type string, number, null (there may be others).
o A code sample that can reproduce the issue (in email only if preferred)
Here is a simple JSON-based web server that responds with an error code provided with the client request.
import BaseHTTPServer
import json
class MockRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def init(self):
pass
def do_GET(self):
basename, extension = self.path.split('/')[1].split('.')
message_body = {
"array" : [ 1, 2, 3 ],
"dict" : { 1 : "2" },
"float" : 1.23,
"number": "123",
"string": "hello"
}.get(basename);
if extension == "json":
content_type = "application/json"
message_body = json.dumps(message_body)
else:
content_type = "text/plain"
message_body = str(message_body)
self.send_response(200)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", len(message_body))
self.end_headers()
self.wfile.write(message_body)
def _main():
server_address = ('', 8000)
httpd = BaseHTTPServer.HTTPServer(server_address, MockRequestHandler)
httpd.serve_forever()
if __name__=='__main__':
_main()
This is the client code..
using Toybox.Application as App;
using Toybox.Lang as Lang;
using Toybox.System as Sys;
using Toybox.WatchUi as Ui;
using Toybox.Graphics as Gfx;
using Toybox.Communications as Comm;
var _G_code;
var _G_data;
var _G_pending = false;
var _G_files = [
"float.json",
"number.json",
"string.json",
"array.json",
"dict.json",
"string.txt",
"float.txt",
"number.txt"
];
var _G_index = 0;
class XBehaviorDelegate extends Ui.BehaviorDelegate
{
function initialize() {
BehaviorDelegate.initialize();
}
function onSelect() {
if (!_G_pending) {
_G_pending = true;
_G_code = "";
_G_data = _G_files[_G_index];
var url = Lang.format("localhost:8080/$1$", [ _G_data ]);
_G_index = (_G_index + 1) % _G_files.size();
var params = {
};
var options = {
};
Comm.makeWebRequest(url, params, options, method(:onWebResponse));
Ui.requestUpdate();
}
return true;
}
function onWebResponse(code, data) {
_G_pending = false;
_G_code = code;
_G_data = data;
Ui.requestUpdate();
}
}
class XView extends Ui.View
{
function initialize() {
View.initialize();
}
function onUpdate(dc) {
dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
dc.clear();
var cy = dc.getHeight() / 2;
var cx = dc.getWidth() / 2;
var color = Gfx.COLOR_GREEN;
if (_G_pending) {
color = Gfx.COLOR_RED;
}
dc.setColor(color, color);
dc.fillCircle(cx, 10, 7);
dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
if (_G_code != null) {
var fy = dc.getFontHeight(Gfx.FONT_LARGE);
dc.drawText(cx, cy - fy, Gfx.FONT_LARGE, _G_code.toString(), Gfx.TEXT_JUSTIFY_CENTER);
if (_G_data != null) {
dc.drawText(cx, cy, Gfx.FONT_XTINY, _G_data.toString(), Gfx.TEXT_JUSTIFY_CENTER);
}
}
}
}
class XApp extends App.AppBase
{
function initialize() {
AppBase.initialize();
}
function getInitialView() {
return [ new XView(), new XBehaviorDelegate() ];
}
}
Travis