Search in sources :

Example 56 with Response

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;
}
Also used : Response(com.squareup.okhttp.Response) HashMap(java.util.HashMap) Request(com.android.volley.Request) IOException(java.io.IOException)

Example 57 with Response

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));
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) InputStreamReader(java.io.InputStreamReader) Request(com.squareup.okhttp.Request) BufferedReader(java.io.BufferedReader) RequestBody(com.squareup.okhttp.RequestBody)

Example 58 with Response

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();
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request)

Example 59 with Response

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);
        }
    });
}
Also used : Response(com.squareup.okhttp.Response) Callback(com.squareup.okhttp.Callback) QAHttpCallback(cn.jeesoft.qa.libcore.http.QAHttpCallback) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Example 60 with Response

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());
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) BufferedSink(okio.BufferedSink) File(java.io.File)

Aggregations

Response (com.squareup.okhttp.Response)109 Request (com.squareup.okhttp.Request)75 IOException (java.io.IOException)70 OkHttpClient (com.squareup.okhttp.OkHttpClient)31 RequestBody (com.squareup.okhttp.RequestBody)23 JSONException (org.json.JSONException)16 JSONObject (org.json.JSONObject)15 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 ApiCallException (org.eyeseetea.malariacare.domain.exception.ApiCallException)10 Callback (com.squareup.okhttp.Callback)9 SocketTimeoutException (java.net.SocketTimeoutException)8 File (java.io.File)7 ShowException (org.eyeseetea.malariacare.views.ShowException)7 Buffer (okio.Buffer)6 MediaType (com.squareup.okhttp.MediaType)5 InputStream (java.io.InputStream)5 HashMap (java.util.HashMap)5 Call (com.squareup.okhttp.Call)4 ApiException (io.kubernetes.client.ApiException)4