use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.
the class BasicAuthenticator method getServerVersion.
/**
* Returns the version of the given server.
* Null if something went wrong
*/
public static String getServerVersion(String url) {
String serverVersion;
try {
String urlServerInfo = url + DHIS_SERVER_INFO;
Response response = executeCall(null, urlServerInfo, "GET");
//Error -> null
if (!response.isSuccessful()) {
Log.e(TAG, "getServerVersion (" + response.code() + "): " + response.body().string());
throw new IOException(response.message());
}
JSONObject data = parseResponse(response.body().string());
serverVersion = data.getString(TAG_VERSION);
} catch (Exception ex) {
Log.e(TAG, "getServerVersion: " + ex.toString());
serverVersion = "";
}
Log.i(TAG, String.format("getServerVersion(%s) -> %s", url, serverVersion));
return serverVersion;
}
use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.
the class BasicAuthenticator method pullOrgUnitsCodes.
/**
* This method returns a String[] whit the Organitation codes
*/
public static String[] pullOrgUnitsCodes(String url) {
try {
String orgUnitsURL = getDhisOrgUnitsURL(url);
Response response = executeCall(null, orgUnitsURL, "GET");
//Error -> null
if (!response.isSuccessful()) {
Log.e(TAG, "pullOrgUnitsCodes (" + response.code() + "): " + response.body().string());
throw new IOException(response.message());
}
//{"organisationUnits":[{}]}
JSONArray orgUnitsArray = parseResponse(response.body().string()).getJSONArray(TAG_ORGANISATIONUNITS);
//0 matches -> Error
if (orgUnitsArray.length() == 0) {
throw new Exception("Found 0 matches");
}
return Utils.jsonArrayToStringArray(orgUnitsArray, TAG_CODE);
} catch (Exception ex) {
Log.e(TAG, String.format("pullOrgUnitsCodes(%url): %s", url, ex.getMessage()));
String[] value = new String[1];
value[0] = "";
return value;
}
}
use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.
the class BasicAuthenticator method patchClosedDate.
/**
* Updates the orgUnit adding a closedDate
*/
static void patchClosedDate(String url, String orgUnitUID) {
//https://malariacare.psi.org/api/organisationUnits/u5jlxuod8xQ/closedDate
try {
String urlPathClosedDate = getPatchClosedDateUrl(url, orgUnitUID);
JSONObject data = prepareTodayDateValue();
Response response = executeCall(data, urlPathClosedDate, "PATCH");
if (!response.isSuccessful()) {
Log.e(TAG, "closingDatePatch (" + response.code() + "): " + response.body().string());
throw new IOException(response.message());
}
} catch (Exception e) {
Log.e(TAG, String.format("patchClosedDate(%s,%s): %s", url, orgUnitUID, e.getMessage()));
}
}
use of com.squareup.okhttp.Response in project weex-example by KalicyZhou.
the class DefaultWebSocketAdapter method connect.
@Override
public void connect(String url, @Nullable String protocol, EventListener listener) {
this.eventListener = listener;
OkHttpClient okHttpClient = new OkHttpClient();
Request.Builder builder = new Request.Builder();
if (protocol != null) {
builder.addHeader(HEADER_SEC_WEBSOCKET_PROTOCOL, protocol);
}
builder.url(url);
WebSocketCall.create(okHttpClient, builder.build()).enqueue(new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Request request, Response response) throws IOException {
ws = webSocket;
eventListener.onOpen();
}
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
eventListener.onMessage(payload.readUtf8());
payload.close();
}
@Override
public void onPong(Buffer payload) {
}
@Override
public void onClose(int code, String reason) {
eventListener.onClose(code, reason, true);
}
@Override
public void onFailure(IOException e) {
e.printStackTrace();
if (e instanceof EOFException) {
eventListener.onClose(WebSocketCloseCodes.CLOSE_NORMAL.getCode(), WebSocketCloseCodes.CLOSE_NORMAL.name(), true);
} else {
eventListener.onError(e.getMessage());
}
}
});
}
use of com.squareup.okhttp.Response in project ignite by apache.
the class IgniteCamelStreamerTest method sendMessages.
/**
* @throws IOException
* @return HTTP response payloads.
*/
private List<String> sendMessages(int fromIdx, int cnt, boolean singleMessage) throws IOException {
List<String> responses = Lists.newArrayList();
if (singleMessage) {
StringBuilder sb = new StringBuilder();
for (int i = fromIdx; i < fromIdx + cnt; i++) sb.append(i).append(",").append(TEST_DATA.get(i)).append("\n");
Request request = new Request.Builder().url(url).post(RequestBody.create(TEXT_PLAIN, sb.toString())).build();
Response response = httpClient.newCall(request).execute();
responses.add(response.body().string());
} else {
for (int i = fromIdx; i < fromIdx + cnt; i++) {
String payload = i + "," + TEST_DATA.get(i);
Request request = new Request.Builder().url(url).post(RequestBody.create(TEXT_PLAIN, payload)).build();
Response response = httpClient.newCall(request).execute();
responses.add(response.body().string());
}
}
return responses;
}
Aggregations