use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.
the class ServerAPIController method getOrgUnitData.
/**
* Returns the orgunit data from the given server according to its current version
*/
static JSONObject getOrgUnitData(String url, String orgUnitNameOrCode) throws ApiCallException {
// Version is required to choose which field to match
String serverVersion = getServerVersion(url);
// No version -> No data
if (serverVersion == null) {
return null;
}
String urlOrgUnitData = getOrgUnitDataUrl(url, serverVersion, orgUnitNameOrCode);
Response response = ServerApiCallExecution.executeCall(null, urlOrgUnitData, "GET");
// {"organisationUnits":[{}]}
JSONObject jsonResponse = ServerApiUtils.getApiResponseAsJSONObject(response);
JSONArray orgUnitsArray = null;
try {
orgUnitsArray = (JSONArray) jsonResponse.get(TAG_ORGANISATIONUNITS);
} catch (JSONException e) {
throw new ApiCallException(e);
}
// 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;
}
try {
return (JSONObject) orgUnitsArray.get(0);
} catch (JSONException e) {
throw new ApiCallException(e);
}
}
use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.
the class ServerAPIController method getOrganisationUnitsByCode.
public static OrganisationUnit getOrganisationUnitsByCode(String code) throws ApiCallException {
// Version is required to choose which field to match
String serverVersion = getServerVersion(PreferencesState.getInstance().getDhisURL());
// No version -> No data
if (serverVersion == null) {
return null;
}
try {
String urlOrgUnitData = getOrganisationUnitsCredentialsUrl(code);
if (!isNetworkAvailable()) {
throw new NetworkException();
}
Response response = ServerApiCallExecution.executeCall(null, urlOrgUnitData, "GET");
JSONObject body = ServerApiUtils.getApiResponseAsJSONObject(response);
// {"organisationUnits":[{}]}
JSONArray orgUnitsArray = (JSONArray) body.get(TAG_ORGANISATIONUNITS);
// 0| >1 matches -> Error
if (orgUnitsArray.length() == 0 || orgUnitsArray.length() > 1) {
Log.e(TAG, String.format("getOrgUnitData(%s) -> Found %d matches", code, orgUnitsArray.length()));
return null;
}
JSONObject orgUnitJO = (JSONObject) orgUnitsArray.get(0);
return parseOrgUnit(orgUnitJO);
} catch (NetworkException e) {
throw new ApiCallException(e);
} catch (Exception ex) {
throw new ApiCallException(ex);
}
}
use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.
the class BasicAuthenticator method executeCall.
/**
* Call to DHIS Server
*/
static Response executeCall(JSONObject data, String url, String method) throws ApiCallException {
final String DHIS_URL = url;
Log.d(method, DHIS_URL);
OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient();
BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
client.setAuthenticator(basicAuthenticator);
Request.Builder builder = new Request.Builder().header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL);
switch(method) {
case "POST":
RequestBody postBody = RequestBody.create(JSON, data.toString());
builder.post(postBody);
break;
case "PUT":
RequestBody putBody = RequestBody.create(JSON, data.toString());
builder.put(putBody);
break;
case "PATCH":
RequestBody patchBody = RequestBody.create(JSON, data.toString());
builder.patch(patchBody);
break;
case "GET":
builder.get();
break;
}
Request request = builder.build();
Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException ex) {
throw new ApiCallException(ex);
}
return response;
}
use of com.squareup.okhttp.Response in project nmid-headline by miao1007.
the class WebViewFragment method trySetupWebview.
private void trySetupWebview() {
// http://202.202.43.205:8086/api/android/newscontent?category=1&id=194
url = HeadlineService.END_POINT + "/api/android/newscontent?id=" + feed.getIdmember() + "&category=" + feed.getCategory();
WebSettings settings = mWebView.getSettings();
mWebView.setWebContentsDebuggingEnabled(true);
settings.setTextZoom(WebViewPref.getWebViewTextZoom(getActivity()));
switch(WebViewPref.isAutoLoadImages(getActivity())) {
case 0:
settings.setLoadsImagesAutomatically(false);
break;
case 1:
break;
case 2:
if (!NetworkUtils.isWifiAviliable(getActivity())) {
settings.setLoadsImagesAutomatically(false);
}
break;
}
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
String htmlData;
if (ThemePref.isNightMode(getActivity())) {
// Webview will use asserts/style_night.css
htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style_night.css\" /> <body class= \"gloable\"> " + response.body().string() + "</body>";
} else {
// Webview will use asserts/style.css
htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" /> <body class= \"gloable\"> " + response.body().string() + "</body>";
}
Log.d(TAG, Thread.currentThread().getName());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView.loadDataWithBaseURL("file:///android_asset/", htmlData, MIME_TYPE, ENCODING, null);
}
});
}
});
}
use of com.squareup.okhttp.Response in project glide by bumptech.
the class OkHttpStreamFetcher method loadData.
@Override
public void loadData(@NonNull Priority priority, @NonNull final DataCallback<? super InputStream> callback) {
Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
String key = headerEntry.getKey();
requestBuilder.addHeader(key, headerEntry.getValue());
}
Request request = requestBuilder.build();
client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
@Override
public void onFailure(Request request, IOException e) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "OkHttp failed to obtain result", e);
}
callback.onLoadFailed(e);
}
@Override
public void onResponse(Response response) throws IOException {
responseBody = response.body();
if (response.isSuccessful()) {
long contentLength = responseBody.contentLength();
stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
callback.onDataReady(stream);
} else {
callback.onLoadFailed(new HttpException(response.message(), response.code()));
}
}
});
}
Aggregations