Search in sources :

Example 71 with Request

use of okhttp3.Request in project AntennaPod by AntennaPod.

the class GpodnetService method uploadSubscriptions.

/**
     * Uploads the subscriptions of a specific device.
     * <p/>
     * This method requires authentication.
     *
     * @param username      The username. Must be the same user as the one which is
     *                      currently logged in.
     * @param deviceId      The ID of the device whose subscriptions should be updated.
     * @param subscriptions A list of feed URLs containing all subscriptions of the
     *                      device.
     * @throws IllegalArgumentException              If username, deviceId or subscriptions is null.
     * @throws GpodnetServiceAuthenticationException If there is an authentication error.
     */
public void uploadSubscriptions(@NonNull String username, @NonNull String deviceId, @NonNull List<String> subscriptions) throws GpodnetServiceException {
    try {
        URL url = new URI(BASE_SCHEME, BASE_HOST, String.format("/subscriptions/%s/%s.txt", username, deviceId), null).toURL();
        StringBuilder builder = new StringBuilder();
        for (String s : subscriptions) {
            builder.append(s);
            builder.append("\n");
        }
        RequestBody body = RequestBody.create(TEXT, builder.toString());
        Request.Builder request = new Request.Builder().put(body).url(url);
        executeRequest(request);
    } catch (MalformedURLException | URISyntaxException e) {
        e.printStackTrace();
        throw new GpodnetServiceException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Request(okhttp3.Request) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) RequestBody(okhttp3.RequestBody)

Example 72 with Request

use of okhttp3.Request in project AntennaPod by AntennaPod.

the class GpodnetService method getTopTags.

/**
     * Returns the [count] most used tags.
     */
public List<GpodnetTag> getTopTags(int count) throws GpodnetServiceException {
    URL url;
    try {
        url = new URI(BASE_SCHEME, BASE_HOST, String.format("/api/2/tags/%d.json", count), null).toURL();
    } catch (MalformedURLException | URISyntaxException e) {
        e.printStackTrace();
        throw new GpodnetServiceException(e);
    }
    Request.Builder request = new Request.Builder().url(url);
    String response = executeRequest(request);
    try {
        JSONArray jsonTagList = new JSONArray(response);
        List<GpodnetTag> tagList = new ArrayList<>(jsonTagList.length());
        for (int i = 0; i < jsonTagList.length(); i++) {
            JSONObject jObj = jsonTagList.getJSONObject(i);
            String title = jObj.getString("title");
            String tag = jObj.getString("tag");
            int usage = jObj.getInt("usage");
            tagList.add(new GpodnetTag(title, tag, usage));
        }
        return tagList;
    } catch (JSONException e) {
        e.printStackTrace();
        throw new GpodnetServiceException(e);
    }
}
Also used : GpodnetTag(de.danoeh.antennapod.core.gpoddernet.model.GpodnetTag) MalformedURLException(java.net.MalformedURLException) Request(okhttp3.Request) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) JSONObject(org.json.JSONObject)

Example 73 with Request

use of okhttp3.Request in project AntennaPod by AntennaPod.

the class GpodnetService method searchPodcasts.

/**
     * Searches the podcast directory for a given string.
     *
     * @param query          The search query
     * @param scaledLogoSize The size of the logos that are returned by the search query.
     *                       Must be in range 1..256. If the value is out of range, the
     *                       default value defined by the gpodder.net API will be used.
     */
public List<GpodnetPodcast> searchPodcasts(String query, int scaledLogoSize) throws GpodnetServiceException {
    String parameters = (scaledLogoSize > 0 && scaledLogoSize <= 256) ? String.format("q=%s&scale_logo=%d", query, scaledLogoSize) : String.format("q=%s", query);
    try {
        URL url = new URI(BASE_SCHEME, null, BASE_HOST, -1, "/search.json", parameters, null).toURL();
        Request.Builder request = new Request.Builder().url(url);
        String response = executeRequest(request);
        JSONArray jsonArray = new JSONArray(response);
        return readPodcastListFromJSONArray(jsonArray);
    } catch (JSONException | MalformedURLException e) {
        e.printStackTrace();
        throw new GpodnetServiceException(e);
    } catch (URISyntaxException e) {
        e.printStackTrace();
        throw new IllegalStateException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Request(okhttp3.Request) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL)

Example 74 with Request

use of okhttp3.Request in project amhttp by Eddieyuan123.

the class RequestManager method upload.

public <T> void upload(Context context, String url, CacheControl cacheControl, HashMap<String, String> headers, File file, String fileName, HashMap<String, String> params, Object tag, final OnUploadListener<T> listener) {
    try {
        IRequestBodyFactory requestBodyFactory = new RequestBodyFactoryImpl();
        RequestBody requestBody = requestBodyFactory.buildRequestBody(file, fileName, params);
        final Request request = new Request.Builder().cacheControl(cacheControl == null ? CacheControl.FORCE_NETWORK : cacheControl).headers(Headers.of(headers)).tag(tag == null ? context.hashCode() : tag).url(url).post(new ProgressRequestBody(requestBody, new ProgressRequestListener() {

            @Override
            public void onRequestProgress(final long bytesWritten, final long contentLength, final boolean done) {
                if (listener != null) {
                    Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
                    dispatcher.setRequestListener(listener);
                    Message message = Message.obtain();
                    message.what = MessageConstant.MESSAGE_UPLOAD_PROGRESS;
                    Bundle bundle = new Bundle();
                    bundle.putLong("bytesWritten", bytesWritten);
                    bundle.putLong("contentLength", contentLength);
                    bundle.putBoolean("done", done);
                    message.obj = bundle;
                    dispatcher.sendMessage(message);
                }
            }
        })).build();
        RequestUtils.enqueue(mOkHttpClient, request, listener);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Message(android.os.Message) Bundle(android.os.Bundle) Request(okhttp3.Request) ProgressRequestListener(io.chelizi.amokhttp.upload.ProgressRequestListener) ProgressRequestBody(io.chelizi.amokhttp.upload.ProgressRequestBody) ProgressRequestBody(io.chelizi.amokhttp.upload.ProgressRequestBody) RequestBody(okhttp3.RequestBody)

Example 75 with Request

use of okhttp3.Request in project WordPress-Android by wordpress-mobile.

the class GravatarApi method createClient.

private static OkHttpClient createClient(final String accessToken) {
    OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
    //// uncomment the following line to add logcat logging
    //httpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
    // add oAuth token usage
    httpClientBuilder.addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request original = chain.request();
            Request.Builder requestBuilder = original.newBuilder().header("Authorization", "Bearer " + accessToken).method(original.method(), original.body());
            Request request = requestBuilder.build();
            return chain.proceed(request);
        }
    });
    return httpClientBuilder.build();
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Interceptor(okhttp3.Interceptor)

Aggregations

Request (okhttp3.Request)1552 Response (okhttp3.Response)1090 Test (org.junit.Test)948 IOException (java.io.IOException)624 MockResponse (okhttp3.mockwebserver.MockResponse)560 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)556 OkHttpClient (okhttp3.OkHttpClient)343 RequestBody (okhttp3.RequestBody)281 Call (okhttp3.Call)255 ResponseBody (okhttp3.ResponseBody)252 HttpUrl (okhttp3.HttpUrl)186 Test (org.junit.jupiter.api.Test)158 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)142 Buffer (okio.Buffer)138 List (java.util.List)114 Callback (okhttp3.Callback)114 File (java.io.File)105 URI (java.net.URI)101 InputStream (java.io.InputStream)99 JSONObject (org.json.JSONObject)96