为什么要跳过 onResponse 方法?
Why is this skipping the onResponse method?
我想从 JSON 获取数据然后 return 它在一个数组中,但是数组总是 return 为 null,因为我的程序不会进入 onResponse方法。
我想在 RecyclerView 中显示此数据。一开始还行,现在不行了,不知道为什么...
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 服务的调用是异步的,所以当你使用 "return" 时,数组是空的,当你的数据在结果中时并不重要,因为你已经加载了 RecyclerView。解决方案是你实现一个回调函数或者在 RecyclerView 中使用 notifydatasetchanged。
检查链接。
Using callbacks in android
Using 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-----------------------------------------------------
我想从 JSON 获取数据然后 return 它在一个数组中,但是数组总是 return 为 null,因为我的程序不会进入 onResponse方法。
我想在 RecyclerView 中显示此数据。一开始还行,现在不行了,不知道为什么...
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 服务的调用是异步的,所以当你使用 "return" 时,数组是空的,当你的数据在结果中时并不重要,因为你已经加载了 RecyclerView。解决方案是你实现一个回调函数或者在 RecyclerView 中使用 notifydatasetchanged。
检查链接。
Using callbacks in android
Using 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-----------------------------------------------------