use of okhttp3.OkHttpClient in project AntennaPod by AntennaPod.
the class ItunesSearchFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_itunes_search, container, false);
gridView = (GridView) root.findViewById(R.id.gridView);
adapter = new ItunesAdapter(getActivity(), new ArrayList<>());
gridView.setAdapter(adapter);
//Show information about the podcast when the list item is clicked
gridView.setOnItemClickListener((parent, view1, position, id) -> {
Podcast podcast = searchResults.get(position);
if (!podcast.feedUrl.contains("itunes.apple.com")) {
Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, podcast.feedUrl);
intent.putExtra(OnlineFeedViewActivity.ARG_TITLE, "iTunes");
startActivity(intent);
} else {
gridView.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
subscription = Observable.create((Observable.OnSubscribe<String>) subscriber -> {
OkHttpClient client = AntennapodHttpClient.getHttpClient();
Request.Builder httpReq = new Request.Builder().url(podcast.feedUrl).header("User-Agent", ClientConfig.USER_AGENT);
try {
Response response = client.newCall(httpReq.build()).execute();
if (response.isSuccessful()) {
String resultString = response.body().string();
JSONObject result = new JSONObject(resultString);
JSONObject results = result.getJSONArray("results").getJSONObject(0);
String feedUrl = results.getString("feedUrl");
subscriber.onNext(feedUrl);
} else {
String prefix = getString(R.string.error_msg_prefix);
subscriber.onError(new IOException(prefix + response));
}
} catch (IOException | JSONException e) {
subscriber.onError(e);
}
subscriber.onCompleted();
}).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(feedUrl -> {
progressBar.setVisibility(View.GONE);
gridView.setVisibility(View.VISIBLE);
Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, feedUrl);
intent.putExtra(OnlineFeedViewActivity.ARG_TITLE, "iTunes");
startActivity(intent);
}, error -> {
Log.e(TAG, Log.getStackTraceString(error));
progressBar.setVisibility(View.GONE);
gridView.setVisibility(View.VISIBLE);
String prefix = getString(R.string.error_msg_prefix);
new MaterialDialog.Builder(getActivity()).content(prefix + " " + error.getMessage()).neutralText(android.R.string.ok).show();
});
}
});
progressBar = (ProgressBar) root.findViewById(R.id.progressBar);
txtvError = (TextView) root.findViewById(R.id.txtvError);
butRetry = (Button) root.findViewById(R.id.butRetry);
txtvEmpty = (TextView) root.findViewById(android.R.id.empty);
loadToplist();
return root;
}
use of okhttp3.OkHttpClient in project AntennaPod by AntennaPod.
the class ItunesSearchFragment method search.
private void search(String query) {
if (subscription != null) {
subscription.unsubscribe();
}
gridView.setVisibility(View.GONE);
txtvError.setVisibility(View.GONE);
butRetry.setVisibility(View.GONE);
txtvEmpty.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
subscription = rx.Observable.create((Observable.OnSubscribe<List<Podcast>>) subscriber -> {
String encodedQuery = null;
try {
encodedQuery = URLEncoder.encode(query, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
if (encodedQuery == null) {
encodedQuery = query;
}
String formattedUrl = String.format(API_URL, query).replace(' ', '+');
OkHttpClient client = AntennapodHttpClient.getHttpClient();
Request.Builder httpReq = new Request.Builder().url(formattedUrl).header("User-Agent", ClientConfig.USER_AGENT);
List<Podcast> podcasts = new ArrayList<>();
try {
Response response = client.newCall(httpReq.build()).execute();
if (response.isSuccessful()) {
String resultString = response.body().string();
JSONObject result = new JSONObject(resultString);
JSONArray j = result.getJSONArray("results");
for (int i = 0; i < j.length(); i++) {
JSONObject podcastJson = j.getJSONObject(i);
Podcast podcast = Podcast.fromSearch(podcastJson);
podcasts.add(podcast);
}
} else {
String prefix = getString(R.string.error_msg_prefix);
subscriber.onError(new IOException(prefix + response));
}
} catch (IOException | JSONException e) {
subscriber.onError(e);
}
subscriber.onNext(podcasts);
subscriber.onCompleted();
}).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(podcasts -> {
progressBar.setVisibility(View.GONE);
updateData(podcasts);
}, error -> {
Log.e(TAG, Log.getStackTraceString(error));
progressBar.setVisibility(View.GONE);
txtvError.setText(error.toString());
txtvError.setVisibility(View.VISIBLE);
butRetry.setOnClickListener(v -> search(query));
butRetry.setVisibility(View.VISIBLE);
});
}
use of okhttp3.OkHttpClient in project twitter4j by yusuke.
the class Http2ClientTest method testHttp2.
public void testHttp2() throws Exception {
AlternativeHttpClientImpl.sPreferSpdy = false;
AlternativeHttpClientImpl.sPreferHttp2 = true;
AlternativeHttpClientImpl http = callOembed();
// check HTTP/2.0
Field f = http.getClass().getDeclaredField("okHttpClient");
f.setAccessible(true);
OkHttpClient client = (OkHttpClient) f.get(http);
assertNotNull("ensure that OkHttpClient is used", client);
ConnectionPool p = client.connectionPool();
assertEquals(1, p.connectionCount());
assertEquals(Protocol.HTTP_2, http.getLastRequestProtocol());
}
use of okhttp3.OkHttpClient in project twitter4j by yusuke.
the class Http2ClientTest method testNoSpdy.
public void testNoSpdy() throws Exception {
AlternativeHttpClientImpl.sPreferSpdy = false;
AlternativeHttpClientImpl.sPreferHttp2 = false;
AlternativeHttpClientImpl http = callOembed();
// check not SPDY
Field f = http.getClass().getDeclaredField("okHttpClient");
f.setAccessible(true);
OkHttpClient client = (OkHttpClient) f.get(http);
ConnectionPool p = client.connectionPool();
assertEquals(1, p.connectionCount());
assertEquals(Protocol.HTTP_1_1, http.getLastRequestProtocol());
}
use of okhttp3.OkHttpClient 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