Java Okhttp3-调用API后的IndexOutOfBoundsException
我是android开发的新手,但我一直无法理解为什么我可以调用我的API,但它没有及时填充我的类,以便填充recycler视图。我得到IndexOutOfBoundsException,因为mData.getDataFeeds()返回null。如果我调试这个应用程序并慢慢地浏览它,它就会工作Java Okhttp3-调用API后的IndexOutOfBoundsException,java,android,okhttp3,Java,Android,Okhttp3,我是android开发的新手,但我一直无法理解为什么我可以调用我的API,但它没有及时填充我的类,以便填充recycler视图。我得到IndexOutOfBoundsException,因为mData.getDataFeeds()返回null。如果我调试这个应用程序并慢慢地浏览它,它就会工作 ListFeedAdapter listFeedAdapter = new ListFeedAdapter(mData.getDataFeeds()); 我有一个活动得到一个片段 @Nullable @O
ListFeedAdapter listFeedAdapter = new ListFeedAdapter(mData.getDataFeeds());
我有一个活动得到一个片段
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
RecyclerView recyclerView = view.findViewById(R.id.listRecyclerView);
try {
login();
getFeed();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
ListFeedAdapter listFeedAdapter = new ListFeedAdapter(mData.getDataFeeds());
recyclerView.setAdapter(listFeedAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
return view;
}
然后我调用login()
getFeed()
okhttp是一种异步操作,您应该在onResponse()之后使用mData
你想让我们调试它?在
mData=parseDataFeed(jsonData)中设置的内容代码>?是的,伙计。。。我要你调试它。。。来吧,数据已经设置好了。当我调试它时,应用程序运行良好。。。当我在没有调试的情况下运行它时,mData似乎没有及时填充。谢谢……就是这样。
private void login() throws IOException {
String user = "user";
String password = "pass";
String loginUrl = getString(R.string.jsonLogin);
OkHttpClient client = new OkHttpClient.Builder()
.build();
JSONObject credentials = new JSONObject();
JSONObject session = new JSONObject();
try {
credentials.put("email", user);
credentials.put("password", password);
session.put("session", credentials);
} catch (JSONException e) {
e.printStackTrace();
}
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, session.toString());
Request request = new Request.Builder()
.url(loginUrl)
.post(body)
.addHeader("Content-Type", mediaType.toString())
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String jsonData = response.body().string();
String jsonHead = response.headers("Set-Cookie").toString();
if(response.isSuccessful()) {
for (String setCookie : response.headers("Set-Cookie")) {
cookies.add(Cookie.parse(response.request().url(), setCookie));
}
}
}
});
private void getFeed() throws IOException, JSONException {
String loginUrl = "http://testurlhere";
OkHttpClient client = new OkHttpClient.Builder()
.build();
MediaType mediaType = MediaType.parse("application/json");
Request request = new Request.Builder()
.url(loginUrl)
.get()
.addHeader("Content-Type", mediaType.toString())
.addHeader("_session", cookies.get(0).toString())
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String jsonData = response.body().string();
String jsonHead = response.headers("Set-Cookie").toString();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mData = parseDataFeed(jsonData);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
updateDisplay();
}
});
}
}
catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (JSONException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});
}
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String jsonData = response.body().string();
String jsonHead = response.headers("Set-Cookie").toString();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mData = parseDataFeed(jsonData);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ListFeedAdapter adapter = new ListFeedAdapter(mData.getDataFeeds());
rexyxlerView.setAdapter(adapter);
updateDisplay();
}
});
}
}
catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (JSONException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});