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);
}
}
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);
}
}
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);
}
}
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();
}
}
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();
}
Aggregations