为什么这跳过了onResponse方法?

为什么这跳过了onResponse方法?

问题描述:

我想从JSON中获取数据,然后将其返回到数组中,但是该数组始终返回null,因为我的程序不会进入onResponse方法.

I want to get data from JSON and then return it in an array, but the array always return with null, because my program won't go into the onResponse method.

我想在RecyclerView中显示此数据.起初它起作用了,但是现在不起作用了,我不知道为什么...

I want to show this data in a RecyclerView. At first it worked but now it won't work I don't know why...

private SzabadEuMusorok[] getSzabadEuMusoroks(){
    if (isNetworkAvaible()){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url("validurl").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 {
                try {
                    String jsonData = response.body().string();
                    Log.v("JSONDATA", jsonData);
                    if(response.isSuccessful()){
                        JSONArray jsonArray = new JSONArray(jsonData);

                        mSzabadEuMusoroks = 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.setVideoURL(mainObject.getString("url"));

                            mSzabadEuMusoroks[i] = szabadEuMusorok;
                        }
                    }
                } catch (JSONException e) {
                    Log.e("JSONEXCEPTION", "Exception caught: ", e);
                }
            }
        });
    }
    else{
        Toast.makeText(this, R.string.network_unavaible_message, Toast.LENGTH_LONG).show();
    }
    return mSzabadEuMusoroks;

您的代码在逻辑上是错误的.因为对Web服务的调用是异步的,所以当您使用返回"时,数组为空,当您的数据处于结果中时,因为您已经加载了RecyclerView,所以没有关系.解决方案将是您实现回调函数或使用RecyclerView中的notifydatasetchanged.

Your code is wrong in the logical sense. Because the call to web service is asynchronous, so when you uses "return", the array is empty, when your data is in the result doesn't matter because you already loaded the RecyclerView. The solution will be that you implement a callback function or use notifydatasetchanged in the RecyclerView.

检查链接.

在android中使用回调

使用notifydatasetchanged

---------- notifydatasetchanged ----------

---------- notifydatasetchanged ----------

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.setVideoURL(mainObject.getString("url"));

  mSzabadEuMusoroks[i] = szabadEuMusorok;
}
//LINE ADDED-----------------------------------------------------
yourAdapterInTheRecyclerView.notifyDataSetChanged();
//LINE ADDED-----------------------------------------------------