use of com.squareup.okhttp.Response in project OkVolley by googolmo.
the class OkHttpStack method performRequest.
/**
* perform the request
*
* @param request request
* @param additionalHeaders headers
* @return http response
* @throws java.io.IOException
* @throws com.android.volley.AuthFailureError
*/
@Override
public Response performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
com.squareup.okhttp.Request.Builder builder = new com.squareup.okhttp.Request.Builder();
builder.url(url);
for (String headerName : map.keySet()) {
builder.header(headerName, map.get(headerName));
// connection.addRequestProperty(headerName, map.get(headerName));
if (VolleyLog.DEBUG) {
// print header message
VolleyLog.d("RequestHeader: %1$s:%2$s", headerName, map.get(headerName));
}
}
setConnectionParametersForRequest(builder, request);
// Initialize HttpResponse with data from the okhttp.
Response okHttpResponse = mClient.newCall(builder.build()).execute();
int responseCode = okHttpResponse.code();
if (responseCode == -1) {
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
return okHttpResponse;
}
use of com.squareup.okhttp.Response in project MusicDNA by harjot-oberai.
the class ViewLyrics method search.
private static ArrayList<Lyrics> search(String searchQuery) throws IOException, ParserConfigurationException, SAXException, NoSuchAlgorithmException {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(10, TimeUnit.SECONDS);
client.setReadTimeout(30, TimeUnit.SECONDS);
RequestBody body = RequestBody.create(MediaType.parse("application/text"), assembleQuery(searchQuery.getBytes("UTF-8")));
Request request = new Request.Builder().header("User-Agent", clientUserAgent).post(body).url(url).build();
Response response = client.newCall(request).execute();
BufferedReader rd = new BufferedReader(new InputStreamReader(response.body().byteStream(), "ISO_8859_1"));
// Get full result
StringBuilder builder = new StringBuilder();
char[] buffer = new char[8192];
int read;
while ((read = rd.read(buffer, 0, buffer.length)) > 0) {
builder.append(buffer, 0, read);
}
String full = builder.toString();
// Decrypt, parse, store, and return the result list
return parseResultXML(decryptResultXML(full));
}
use of com.squareup.okhttp.Response in project MusicDNA by harjot-oberai.
the class Net method getUrlAsString.
public static String getUrlAsString(URL paramURL) throws IOException {
Request request = new Request.Builder().header("User-Agent", USER_AGENT).url(paramURL).build();
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(10, TimeUnit.SECONDS);
Response response = client.newCall(request).execute();
return response.body().string();
}
use of com.squareup.okhttp.Response in project QuickAndroid by ImKarl.
the class OkHttp method load.
/**
* 异步请求
*
* @param method
* @param url
* @param params
* @param listener
* @param <T>
*/
public <T> void load(QAHttpMethod method, final String url, final QARequestParams params, final QAHttpCallback<T> listener) {
final Request request = createRequest(method, url, params, listener);
if (request == null) {
return;
}
if (params != null && params.isPreLoad()) {
// 缓存加载
preLoad(url, params, request, listener);
}
// 网络加载
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(final Request request, final IOException e) {
sendFailedCallback(url, e, listener);
}
@Override
public void onResponse(final Response response) {
handlerResponse(url, params, response, listener);
}
});
}
use of com.squareup.okhttp.Response in project cw-omnibus by commonsguy.
the class RestoreService method onHandleIntent.
@Override
protected void onHandleIntent(Intent i) {
Request request = new Request.Builder().url(i.getData().toString()).build();
try {
Response response = BackupService.OKHTTP_CLIENT.newCall(request).execute();
File toRestore = new File(getCacheDir(), "backup.zip");
if (toRestore.exists()) {
toRestore.delete();
}
BufferedSink sink = Okio.buffer(Okio.sink(toRestore));
sink.writeAll(response.body().source());
sink.close();
ZipUtils.unzip(toRestore, getFilesDir(), BackupService.ZIP_PREFIX_FILES);
ZipUtils.unzip(toRestore, BackupService.getSharedPrefsDir(this), BackupService.ZIP_PREFIX_PREFS);
ZipUtils.unzip(toRestore, getExternalFilesDir(null), BackupService.ZIP_PREFIX_EXTERNAL);
EventBus.getDefault().post(new RestoreCompletedEvent());
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Exception restoring backup", e);
EventBus.getDefault().post(new RestoreFailedEvent());
}
}
Aggregations