仅在我在调试中运行时才有效

时间:2016-09-24 10:07:54

标签: android json asynchronous android-asynctask okhttp

我正在尝试获取JSONData,如果我在调试模式下运行它,它可以工作,但如果没有它将无法工作......我认为我有这个异步的问题,但我不知道不知道出了什么问题。请帮助我试着让它完成2天,没有任何工作......

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

1 个答案:

答案 0 :(得分:0)

请检查此解决方案(source code)