use of java.io.InputStreamReader in project android-demos by novoda.
the class JsonRequest method onResume.
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "Querying droidcon on Twitter", Toast.LENGTH_SHORT).show();
Reader reader = new InputStreamReader(retrieveStream(url));
SearchResponse response = new Gson().fromJson(reader, SearchResponse.class);
List<String> searches = new ArrayList<String>();
Iterator<Result> i = response.results.iterator();
while (i.hasNext()) {
Result res = (Result) i.next();
searches.add(res.text);
}
ListView v = (ListView) findViewById(R.id.list);
v.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, searches.toArray(new String[searches.size()])));
}
use of java.io.InputStreamReader in project openhab1-addons by openhab.
the class GC100IRControlPoint method doAction.
/**
* {@inheritDoc}
*/
@Override
public synchronized GC100IRCommand doAction(String ipAddressString, int module, int connector, String code) {
if (code == null) {
return null;
}
GC100IRDevice device = getGCDevice(ipAddressString, module, connector);
if (device == null) {
logger.warn("Unable to get GC 100 Device for IP Address '{}', module '{}' and connector '{}'.", ipAddressString, module, connector);
return null;
}
GC100IRConnection tcpIPSocket = device.getTCPIPSocket();
if (tcpIPSocket == null) {
if (connectTarget(ipAddressString, module, connector)) {
device = getGCDevice(ipAddressString, module, connector);
if (device == null) {
logger.warn("Unable to get GC 100 Device for IP Address '{}', module '{}' and connector '{}'.", ipAddressString, module, connector);
return null;
}
tcpIPSocket = device.getTCPIPSocket();
if (tcpIPSocket == null) {
logger.warn("Unable to get TCP IP Socket for IP Address '{}', module '{}' and connector '{}'.", ipAddressString, module, connector);
return null;
}
}
}
if (tcpIPSocket == null) {
logger.warn("Unable to get TCP IP Socket for IP Address '{}', module '{}' and connector '{}'.", ipAddressString, module, connector);
return null;
}
String deviceType = tcpIPSocket.getDeviceType();
if (deviceType == null) {
logger.warn("Unable to get Device Type for IP Address '{}', module '{}' and connector '{}'.", ipAddressString, module, connector);
return null;
}
try {
if (deviceType.equals(DEVICE_TYPE_IR)) {
Socket socket = tcpIPSocket.getSocket();
if (socket == null) {
logger.warn("Unable to get Socket for IP Address '{}', module '{}' and connector '{}'.", ipAddressString, module, connector);
return null;
}
if (!tcpIPSocket.isConnected()) {
tcpIPSocket.connect();
}
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (in.ready()) {
in.read();
}
out.write(code.getBytes());
StringBuilder response = new StringBuilder();
int count = 0;
while (!in.ready()) {
count++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
logger.warn("InterruptedException", e);
}
if (count > 20) {
return null;
}
}
while (true) {
response.append((char) in.read());
if (!in.ready()) {
break;
}
}
return responseOfDevice(response.toString());
}
} catch (IOException e) {
logger.warn("IOException", e);
}
return null;
}
use of java.io.InputStreamReader in project openhab1-addons by openhab.
the class NeoHubConnector method sendMessage.
/**
* Sends the message over the network and provides the response to the
* response handler.
*
* @param msg
* message to neohub
* @param handler
* handler to process the response, may be <code>null</code>
* @return result of handler or <code>null</code> if network problem
* occurred
*/
public <T> T sendMessage(final String msg, final ResponseHandler<T> handler) {
final StringBuilder response = new StringBuilder();
final Socket socket = new Socket();
try {
socket.connect(new InetSocketAddress(hostname, port), timeout);
final InputStreamReader in = new InputStreamReader(socket.getInputStream(), US_ASCII);
final OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream(), US_ASCII);
logger.debug(">> {}", msg);
out.write(msg);
// NUL terminate the command string
out.write(0);
out.flush();
int l;
while ((l = in.read()) > 0) {
// NUL termination & end of stream (-1)
response.append((char) l);
}
} catch (final IOException e) {
logger.error("Failed to connect to neohub [host '{}' port '{}' timeout '{}']", new Object[] { hostname, port, timeout });
logger.debug("Failed to connect to neohub.", e);
return null;
} finally {
IOUtils.closeQuietly(socket);
}
final String responseStr = response.toString();
logger.debug("<< {}", responseStr);
if (handler != null) {
return handler.onResponse(responseStr);
} else {
return null;
}
}
use of java.io.InputStreamReader in project openhab1-addons by openhab.
the class MystromClient method ChangeState.
/*
* (non-Javadoc)
*
* @see
* org.openhab.binding.mystromecopower.internal.api.IMystromClient#ChangeState
* (java.lang.String, java.lang.Boolean)
*/
@Override
public Boolean ChangeState(String deviceId, Boolean newStateIsOn) {
Reader reader = null;
logger.debug("Change state for device id '{}', new state is on: '{}'", deviceId, newStateIsOn);
try {
String url = API_URL + "device/switch" + "?authToken=" + this.authToken + "&id=" + deviceId + "&on=" + newStateIsOn.toString();
HttpURLConnection httpURLConnection;
httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
reader = new InputStreamReader(inputStream, "UTF-8");
JsonObject jsonObject = (JsonObject) jsonParser.parse(reader);
String status = jsonObject.get("status").getAsString();
if (!status.equals("ok")) {
String error = jsonObject.get("error").getAsString();
logger.error("Unable to switch state for device '{}' error '{}'", deviceId, error);
return false;
}
String newState = jsonObject.get("state").getAsString();
logger.debug("Switch state for device '{}' successful, state is '{}'", deviceId, newState);
return true;
} catch (Exception ex) {
logger.error("Error set state: '{}'", ex.toString());
return false;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
}
use of java.io.InputStreamReader in project openhab1-addons by openhab.
the class MystromClient method getDevices.
private List<MystromDevice> getDevices(boolean minimalMode) {
Reader reader = null;
logger.debug("get all devices state");
try {
String url = API_URL + "devices" + "?authToken=" + this.authToken;
if (minimalMode) {
url = url + "&minimal=true";
}
HttpURLConnection httpURLConnection;
httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
httpURLConnection.connect();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
logger.error("Get devices http code: '{}'", responseCode);
return new ArrayList<MystromDevice>();
}
InputStream inputStream = httpURLConnection.getInputStream();
reader = new InputStreamReader(inputStream, "UTF-8");
JsonObject jsonObject = (JsonObject) jsonParser.parse(reader);
String status = jsonObject.get("status").getAsString();
if (!status.equals("ok")) {
logger.error("Error while getting devices: '{}'", status);
return new ArrayList<MystromDevice>();
}
GetDevicesResult result = gson.fromJson(jsonObject, GetDevicesResult.class);
logger.debug("Devices discovery sucessfull, found '{}' devices", result.devices.size());
return result.devices;
} catch (Exception ex) {
logger.error("Error getting devices: '{}'", ex.toString());
return new ArrayList<MystromDevice>();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
}
Aggregations