I'm trying to get JSONData, and if I run it in Debug mode, it works, but if not It won't work... I think think that I have a problem with this asynchronous thing, but I don't know what is wrong. Please help I'm trying to get this done for 2 days and nothing will work...
public static final String SZABAD_EU = "SZABAD_EU";
public static Collections collections = new Collections();
CollectionsAdapter adapter;
@InjectView(R.id.archivumReyclerView)
RecyclerView mRecyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_archivum);
ButterKnife.inject(this);
setSzabadEuMusoroks();
adapter = new CollectionsAdapter(this);
mRecyclerView.setAdapter(adapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
}
public void setSzabadEuMusoroks(){
if (isNetworkAvaible()){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://1956.osaarchivum.org/api/items?collection=13").build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
alertUserAboutError();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String jsonData = response.body().string();
Log.v("JSONDATA", jsonData);
if (response.isSuccessful()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
parseData(jsonData);
} catch (JSONException e) {
Log.e("JSONEXCEPTION", "Exception caught: ", e);
}
}
});
} else {
alertUserAboutError();
}
}
});
}
else{
Toast.makeText(this, R.string.network_unavaible_message, Toast.LENGTH_LONG).show();
}
private void parseData(String jsonData) throws JSONException{
JSONArray jsonArray = new JSONArray(jsonData);
SzabadEuMusorok[] szabadEuMusoroks = new SzabadEuMusorok[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
SzabadEuMusorok szabadEuMusorok = new SzabadEuMusorok();
JSONArray elementTexts = jsonObject.getJSONArray("element_texts");
JSONObject titleObject = elementTexts.getJSONObject(0);
szabadEuMusorok.setTitile(titleObject.getString("text"));
JSONObject subjectObject = elementTexts.getJSONObject(3);
szabadEuMusorok.setSubject(subjectObject.getString("text"));
JSONObject mainObject = jsonObject.getJSONObject("files");
szabadEuMusorok.setAudioURL(mainObject.getString("url"));
szabadEuMusoroks[i] = szabadEuMusorok;
}
collections.setSzabadEuMusoroks(szabadEuMusoroks);
}