List of usage examples for com.squareup.okhttp Response isSuccessful
public boolean isSuccessful()
From source file:org.eyeseetea.malariacare.network.ServerAPIController.java
License:Open Source License
/** * Returns the version of the given server. * Null if something went wrong/*from w w w. j a v a2 s . c o m*/ * @param url * @return */ 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; }
From source file:org.eyeseetea.malariacare.network.ServerAPIController.java
License:Open Source License
/** * Returns if the given url contains the current program * @param url/* w w w . jav a 2s.c o m*/ * @return */ public static boolean isValidProgram(String url) { Log.d(TAG, String.format("isValidProgram(%s) ...", url)); String programUIDInServer; try { String urlValidProgram = getIsValidProgramUrl(url); Response response = executeCall(null, urlValidProgram, "GET"); //Error -> null if (!response.isSuccessful()) { Log.e(TAG, "isValidProgram (" + response.code() + "): " + response.body().string()); throw new IOException(response.message()); } JSONObject data = parseResponse(response.body().string()); programUIDInServer = String.valueOf(data.get(TAG_ID)); } catch (Exception ex) { Log.e(TAG, "isValidProgram: " + ex.toString()); return false; } boolean valid = getProgramUID() != null && getProgramUID().equals(programUIDInServer); Log.d(TAG, String.format("isValidProgram(%s) -> %b", url, valid)); return valid; }
From source file:org.eyeseetea.malariacare.network.ServerAPIController.java
License:Open Source License
/** * This method returns a String[] whit the Organitation codes * @throws Exception// www . j a v a 2s . c om */ 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":[{}]} JSONObject jsonResponse = parseResponse(response.body().string()); JSONArray orgUnitsArray = (JSONArray) jsonResponse.get(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; } }
From source file:org.eyeseetea.malariacare.network.ServerAPIController.java
License:Open Source License
/** * Updates the orgUnit adding a closedDate * @param url//from w w w . j av a 2 s. c om * @param orgUnitUID */ 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())); } }
From source file:org.eyeseetea.malariacare.network.ServerAPIController.java
License:Open Source License
static void patchDescriptionClosedDate(String url, String orgUnitUID, String orgUnitDescription) { //https://malariacare.psi.org/api/organisationUnits/u5jlxuod8xQ/closedDate try {/*from w w w . j av a 2 s . c o m*/ String urlPathClosedDescription = getPatchClosedDescriptionUrl(url, orgUnitUID); JSONObject data = prepareClosingDescriptionValue(orgUnitDescription); Response response = executeCall(data, urlPathClosedDescription, "PATCH"); if (!response.isSuccessful()) { Log.e(TAG, "patchDescriptionClosedDate (" + response.code() + "): " + response.body().string()); throw new IOException(response.message()); } } catch (Exception e) { Log.e(TAG, String.format("patchDescriptionClosedDate(%s,%s): %s", url, orgUnitUID, e.getMessage())); } }
From source file:org.eyeseetea.malariacare.network.ServerAPIController.java
License:Open Source License
/** * Returns the orgunit data from the given server according to its current version * @param url//from w ww .jav a 2s . c om * @param orgUnitNameOrCode * @return */ static JSONObject getOrgUnitData(String url, String orgUnitNameOrCode) { //Version is required to choose which field to match String serverVersion = getServerVersion(url); //No version -> No data if (serverVersion == null) { return null; } try { String urlOrgUnitData = getOrgUnitDataUrl(url, serverVersion, orgUnitNameOrCode); Response response = executeCall(null, urlOrgUnitData, "GET"); //Error -> null if (!response.isSuccessful()) { Log.e(TAG, "getOrgUnitData (" + response.code() + "): " + response.body().string()); throw new IOException(response.message()); } //{"organisationUnits":[{}]} JSONObject jsonResponse = parseResponse(response.body().string()); JSONArray orgUnitsArray = (JSONArray) jsonResponse.get(TAG_ORGANISATIONUNITS); //0| >1 matches -> Error if (orgUnitsArray.length() == 0 || orgUnitsArray.length() > 1) { Log.e(TAG, String.format("getOrgUnitData(%s,%s) -> Found %d matches", url, orgUnitNameOrCode, orgUnitsArray.length())); return null; } return (JSONObject) orgUnitsArray.get(0); } catch (Exception ex) { Log.e(TAG, String.format("getOrgUnitData(%s,%s): %s", url, orgUnitNameOrCode, ex.toString())); return null; } }
From source file:org.floens.chan.core.http.HttpCall.java
License:Open Source License
@Override public void onResponse(Response response) { try {//w ww. j a v a 2s. co m if (response.isSuccessful() && response.body() != null) { String responseString = response.body().string(); process(response, responseString); successful = true; } else { onFailure(response.request(), null); } } catch (IOException e) { Logger.e(TAG, "IOException processing response", e); } finally { IOUtils.closeQuietly(response.body()); } AndroidUtils.runOnUiThread(new Runnable() { @Override public void run() { postUI(successful); } }); }
From source file:org.floens.chan.core.manager.ReplyManager.java
License:Open Source License
public void postPass(String token, String pin, final PassListener passListener) { FormEncodingBuilder formBuilder = new FormEncodingBuilder(); formBuilder.add("act", "do_login"); formBuilder.add("id", token); formBuilder.add("pin", pin); Request.Builder request = new Request.Builder().url(ChanUrls.getPassUrl()).post(formBuilder.build()); makeOkHttpCall(request, new Callback() { @Override/*from ww w. j av a2 s. c om*/ public void onFailure(Request request, IOException e) { final PassResponse res = new PassResponse(); res.isError = true; res.message = context.getString(R.string.pass_error); runUI(new Runnable() { public void run() { passListener.onResponse(res); } }); } @Override public void onResponse(Response response) throws IOException { if (!response.isSuccessful()) { onFailure(response.request(), null); return; } String responseString = response.body().string(); response.body().close(); final PassResponse res = new PassResponse(); if (responseString.contains("Your device is now authorized")) { List<String> cookies = response.headers("Set-Cookie"); String passId = null; for (String cookie : cookies) { try { List<HttpCookie> parsedList = HttpCookie.parse(cookie); for (HttpCookie parsed : parsedList) { if (parsed.getName().equals("pass_id") && !parsed.getValue().equals("0")) { passId = parsed.getValue(); } } } catch (IllegalArgumentException ignored) { } } if (passId != null) { res.passId = passId; res.message = "Success! Your device is now authorized."; } else { res.isError = true; res.message = "Could not get pass id"; } } else { res.isError = true; if (responseString.contains("Your Token must be exactly 10 characters")) { res.message = "Incorrect token"; } else if (responseString.contains("You have left one or more fields blank")) { res.message = "You have left one or more fields blank"; } else if (responseString.contains("Incorrect Token or PIN")) { res.message = "Incorrect Token or PIN"; } else { res.unknownError = true; } } runUI(new Runnable() { public void run() { passListener.onResponse(res); } }); } }); }
From source file:org.floens.chan.core.manager.ReplyManager.java
License:Open Source License
public void postDelete(final SavedReply reply, boolean onlyImageDelete, final DeleteListener listener) { FormEncodingBuilder formBuilder = new FormEncodingBuilder(); formBuilder.add(Integer.toString(reply.no), "delete"); if (onlyImageDelete) { formBuilder.add("onlyimgdel", "on"); }//from w w w .j a v a 2s . c o m formBuilder.add("mode", "usrdel"); formBuilder.add("pwd", reply.password); Request.Builder request = new Request.Builder().url(ChanUrls.getDeleteUrl(reply.board)) .post(formBuilder.build()); makeOkHttpCall(request, new Callback() { @Override public void onFailure(Request request, IOException e) { final DeleteResponse res = new DeleteResponse(); res.isNetworkError = true; runUI(new Runnable() { @Override public void run() { listener.onResponse(res); } }); } @Override public void onResponse(Response response) throws IOException { if (!response.isSuccessful()) { onFailure(response.request(), null); return; } String responseString = response.body().string(); response.body().close(); final DeleteResponse res = new DeleteResponse(); res.responseData = responseString; if (responseString.contains("You must wait longer before deleting this post")) { res.isUserError = true; res.isTooSoonError = true; } else if (responseString.contains("Password incorrect")) { res.isUserError = true; res.isInvalidPassword = true; } else if (responseString.contains("You cannot delete a post this old")) { res.isUserError = true; res.isTooOldError = true; } else if (responseString.contains("Updating index")) { res.isSuccessful = true; } runUI(new Runnable() { @Override public void run() { listener.onResponse(res); } }); } }); }
From source file:org.floens.chan.core.manager.ReplyManager.java
License:Open Source License
private void postReplyInternal(final Reply reply, final ReplyListener replyListener, String captchaHash) { reply.password = Long.toHexString(random.nextLong()); MultipartBuilder formBuilder = new MultipartBuilder(); formBuilder.type(MultipartBuilder.FORM); formBuilder.addFormDataPart("mode", "regist"); formBuilder.addFormDataPart("pwd", reply.password); if (reply.resto >= 0) { formBuilder.addFormDataPart("resto", String.valueOf(reply.resto)); }// w w w . j a v a 2s. co m formBuilder.addFormDataPart("name", reply.name); formBuilder.addFormDataPart("email", reply.email); if (!TextUtils.isEmpty(reply.subject)) { formBuilder.addFormDataPart("sub", reply.subject); } formBuilder.addFormDataPart("com", reply.comment); if (captchaHash != null) { formBuilder.addFormDataPart("g-recaptcha-response", captchaHash); } if (reply.file != null) { formBuilder.addFormDataPart("upfile", reply.fileName, RequestBody.create(MediaType.parse("application/octet-stream"), reply.file)); } if (reply.spoilerImage) { formBuilder.addFormDataPart("spoiler", "on"); } Request.Builder request = new Request.Builder().url(ChanUrls.getReplyUrl(reply.board)) .post(formBuilder.build()); if (reply.usePass) { request.addHeader("Cookie", "pass_id=" + reply.passId); } makeOkHttpCall(request, new Callback() { @Override public void onFailure(Request request, IOException e) { final ReplyResponse res = new ReplyResponse(); res.isNetworkError = true; runUI(new Runnable() { public void run() { replyListener.onResponse(res); } }); } @Override public void onResponse(Response response) throws IOException { final ReplyResponse res = new ReplyResponse(); if (response.isSuccessful()) { onReplyPosted(response.body().string(), reply, res); response.body().close(); } else { res.isNetworkError = true; } runUI(new Runnable() { public void run() { replyListener.onResponse(res); } }); } }); }