Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialsamuel zaffran
24,815 PointsWhat's wrong with my code using OkHttp ?
Hi, i would like to call okhttp for different url to load my recyclerView, but don't know what to do so i tried this, but if there is an another solution or a fix.. Please !
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String apiKey = "5e08eafaefd44d14ad70ceea834c16bb";
String url = "https://newsapi.org/v1/articles?source=buzzfeed&apiKey=" +apiKey;
String url1 = "https://newsapi.org/v1/articles?source=polygon&apiKey=" + apiKey;
getInfos(url);
getInfos(url1);
}
private void getInfos(String url) {
if (isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String jsonData = response.body().string();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
Log.v(TAG, jsonData);
mArticles = getArticleForecast(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
getCurrentArticles();
}
});
}
else {
alertUserAboutError();
}
} catch (IOException e) {
e.printStackTrace();} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
alertUserAboutError();
}
});
} else {alertUserAboutError();}
}