List of usage examples for android.webkit WebResourceResponse WebResourceResponse
public WebResourceResponse(String mimeType, String encoding, int statusCode, @NonNull String reasonPhrase, Map<String, String> responseHeaders, InputStream data)
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.GBWebClient.java
private static WebResourceResponse mimicOpenWeatherMapResponse(String type, String units) { if (Weather.getInstance() == null) { LOG.warn("WEBVIEW - Weather instance is null, cannot update weather"); return null; }// www .jav a 2 s . c om CurrentPosition currentPosition = new CurrentPosition(); try { JSONObject resp = Weather.getInstance().createReconstructedOWMWeatherReply(); if ("/data/2.5/weather".equals(type) && resp != null) { JSONObject main = resp.getJSONObject("main"); convertTemps(main, units); //caller might want different units resp.put("cod", 200); resp.put("coord", coordObject(currentPosition)); resp.put("sys", sysObject(currentPosition)); // } else if ("/data/2.5/forecast".equals(type) && Weather.getInstance().getWeather2().reconstructedOWMForecast != null) { //this is wrong, as we only have daily data. Unfortunately it looks like daily forecasts cannot be reconstructed // resp = new JSONObject(Weather.getInstance().getWeather2().reconstructedOWMForecast.toString()); // // JSONObject city = resp.getJSONObject("city"); // city.put("coord", coordObject(currentPosition)); // // JSONArray list = resp.getJSONArray("list"); // for (int i = 0, size = list.length(); i < size; i++) { // JSONObject item = list.getJSONObject(i); // JSONObject main = item.getJSONObject("main"); // convertTemps(main, units); //caller might want different units // } // // resp.put("cod", 200); } else { LOG.warn("WEBVIEW - cannot mimick request of type " + type + " (unsupported or lack of data)"); return null; } LOG.info("WEBVIEW - mimic openweather response" + resp.toString()); Map<String, String> headers = new HashMap<>(); headers.put("Access-Control-Allow-Origin", "*"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return new WebResourceResponse("application/json", "utf-8", 200, "OK", headers, new ByteArrayInputStream(resp.toString().getBytes())); } else { return new WebResourceResponse("application/json", "utf-8", new ByteArrayInputStream(resp.toString().getBytes())); } } catch (JSONException e) { LOG.warn("Error building the JSON weather message.", e); } return null; }