Skip to content

Commit

Permalink
glowna strona nie daje swiezych wynikow, dlatego trzeba pobierac z po…
Browse files Browse the repository at this point in the history
…dstron; todo: polaczyc 4 requesty (rx...)
  • Loading branch information
marioosh-net committed Aug 22, 2016
1 parent 180ed5a commit 23afc18
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
59 changes: 31 additions & 28 deletions app/src/main/java/net/marioosh/stooq/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.List;
import java.util.Locale;

import okhttp3.CacheControl;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -110,31 +111,33 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin

private void fetchData() {
OkHttpClient client = HttpClient.getInstance();
Request request = new Request.Builder()
.url(DATA_URL)
.build();

client.newCall(request).enqueue(new Callback() {
for(final Index.Type t: Index.Type.values()) {
Request request = new Request.Builder()
.cacheControl(CacheControl.FORCE_NETWORK)
.url(t.getSrcUrl())
.build();

@Override
public void onFailure(Call call, final IOException e) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, e+"", Toast.LENGTH_SHORT).show();
}
});
}
client.newCall(request).enqueue(new Callback() {

@Override
public void onResponse(Call call, Response response) throws IOException {
if(!response.isSuccessful()) {
return;
@Override
public void onFailure(Call call, final IOException e) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, e+"", Toast.LENGTH_SHORT).show();
}
});
}

Document document = Jsoup.parse(response.body().string());
@Override
public void onResponse(Call call, Response response) throws IOException {
if(!response.isSuccessful()) {
return;
}

Document document = Jsoup.parse(response.body().string());

for(Index.Type t: Index.Type.values()) {
Elements elements = document.select(t.getCssSelector());
String value = elements.get(0).childNode(0).toString();
Log.d("parsed", t +"="+value);
Expand All @@ -155,16 +158,16 @@ public void onResponse(Call call, Response response) throws IOException {
if(!found) {
data.add(new Index(t, value));
}
}

new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
rv.getAdapter().notifyDataSetChanged();
}
});
}
});
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
rv.getAdapter().notifyDataSetChanged();
}
});
}
});
}
}

private class MyAdapter extends RecyclerView.Adapter<ViewHolder> {
Expand Down
15 changes: 10 additions & 5 deletions app/src/main/java/net/marioosh/stooq/stuff/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@
public class Index {

public enum Type {
WIG("#aq_wig_c2"),
WIG20("#aq_wig20_c2"),
mWIG40("#aq_mwig40_c2"),
sWIG80("#aq_swig80_c2");
WIG("http://stooq.pl/q/?s=wig","#aq_wig_c2"),
WIG20("http://stooq.pl/q/?s=wig20","#aq_wig20_c2"),
mWIG40("http://stooq.pl/q/?s=mwig40","#aq_mwig40_c2"),
sWIG80("http://stooq.pl/q/?s=swig80","#aq_swig80_c2");

private final String cssSelector;
private final String srcUrl;

Type(String cssSelector) {
Type(String srcUrl, String cssSelector) {
this.cssSelector = cssSelector;
this.srcUrl = srcUrl;
}

public String getCssSelector() {
return cssSelector;
}

public String getSrcUrl() {
return srcUrl;
}
}

private Type type;
Expand Down

0 comments on commit 23afc18

Please sign in to comment.