use of com.squareup.okhttp.OkHttpClient in project FreeFlow by Comcast.
the class DribbbleFetch method load.
public void load(final ArtbookActivity caller, int itemsPerPage, int page) {
new AsyncTask<String, Void, String>() {
OkHttpClient client = new OkHttpClient();
private Exception exception;
protected String doInBackground(String... urls) {
try {
return get(new URL(urls[0]));
} catch (Exception e) {
this.exception = e;
Log.e(TAG, "Exception: " + e);
return null;
}
}
protected void onPostExecute(String data) {
DribbbleFeed feed = new Gson().fromJson(data, DribbbleFeed.class);
caller.onDataLoaded(feed);
}
String get(URL url) throws IOException {
HttpURLConnection connection = client.open(url);
InputStream in = null;
try {
in = connection.getInputStream();
byte[] response = readFully(in);
return new String(response, "UTF-8");
} finally {
if (in != null)
in.close();
}
}
byte[] readFully(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int count; (count = in.read(buffer)) != -1; ) {
out.write(buffer, 0, count);
}
return out.toByteArray();
}
}.execute("http://api.dribbble.com/shots/popular?per_page=" + itemsPerPage + "&page=" + page);
}
use of com.squareup.okhttp.OkHttpClient in project ETSMobile-Android2 by ApplETS.
the class AuthentificationPortailTask method doInBackground.
protected Intent doInBackground(String... params) {
OkHttpClient client = new OkHttpClient();
String url = params[0], username = params[1], password = params[2];
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n \"Username\": \"" + username + "\",\n \"Password\": \"" + password + "\"\n}");
Request request = new Request.Builder().url(url).post(body).addHeader("content-type", "application/json").addHeader("cache-control", "no-cache").build();
Response response = null;
String authCookie = "", domaine = "";
int typeUsagerId = 0;
final Intent res = new Intent();
try {
response = client.newCall(request).execute();
if (response.code() == 200) {
authCookie = response.header("Set-Cookie");
JSONObject jsonResponse = new JSONObject(response.body().string());
typeUsagerId = jsonResponse.getInt("TypeUsagerId");
domaine = jsonResponse.getString("Domaine");
res.putExtra(AccountManager.KEY_AUTHTOKEN, authCookie);
res.putExtra(Constants.TYPE_USAGER_ID, typeUsagerId);
res.putExtra(Constants.DOMAINE, domaine);
} else {
Log.e("Erreur Portail", response.toString());
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
res.putExtra(AccountManager.KEY_ACCOUNT_NAME, username);
res.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
res.putExtra(Constants.PARAM_USER_PASS, password);
return res;
}
use of com.squareup.okhttp.OkHttpClient in project ETSMobile-Android2 by ApplETS.
the class AppletsApiEvenementsRequest method loadDataFromNetwork.
@Override
public EvenementCommunauteList loadDataFromNetwork() throws Exception {
String eventsAddress = context.getString(R.string.applets_api_events, source.getKey());
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(eventsAddress).get().build();
Response response = client.newCall(request).execute();
String result = response.body().string();
EvenementCommunauteList evenementList = new Gson().fromJson(result, EvenementCommunauteList.class);
for (EvenementCommunaute event : evenementList) {
event.setSourceEvenement(source);
}
return evenementList;
}
use of com.squareup.okhttp.OkHttpClient in project AisenWeiBo by wangdan.
the class HttpsUtility method getOkHttpClient.
public OkHttpClient getOkHttpClient() {
if (mOKHttpClient == null) {
try {
mOKHttpClient = new OkHttpClient();
mOKHttpClient.setConnectTimeout(GlobalContext.CONN_TIMEOUT, TimeUnit.MILLISECONDS);
mOKHttpClient.setReadTimeout(GlobalContext.READ_TIMEOUT, TimeUnit.MILLISECONDS);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { tm }, null);
mOKHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
} catch (Throwable e) {
e.printStackTrace();
}
}
return mOKHttpClient;
}
use of com.squareup.okhttp.OkHttpClient in project pictureapp by EyeSeeTea.
the class UnsafeOkHttpsClientFactory method getUnsafeOkHttpClient.
public static OkHttpClient getUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
} };
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setSslSocketFactory(sslSocketFactory);
okHttpClient.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations