我想创建一个从服务器获取json响应并设置为我的Recyclerview的应用程序,但它包含错误。
这是我的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_utama, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.list_utra);
dataku = new ArrayList<>();
gridLayoutManager = new GridLayoutManager(getActivity(), 4);
adapter = new RecyclerAdapter(getActivity(), dataku);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(gridLayoutManager);
Ambildatadariserver(0);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
public void onScrolled(RecyclerView recyclerView, int dx, int dy){
if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == dataku.size()-1){
Ambildatadariserver(dataku.get(dataku.size()-1).getId());
}
}
});
return view;
}
private void Ambildatadariserver(int id){
AsyncTask<Integer, String, Void> task = new AsyncTask<Integer, String, Void>(){
@Override
protected Void doInBackground(Integer... integers){
sharedPreferences = getActivity().getSharedPreferences("login.conf", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
String username = sharedPreferences.getString("username", "");
String password = sharedPreferences.getString("password", "");
httpClient = new OkHttpClient();
RequestBody formBody = new FormBody.Builder().add("username_krm", username).add("password_krm", password).build();
Request request = new Request.Builder().url(alamat_json + integers[0]).post(formBody).build();
try {
Response response = httpClient.newCall(request).execute();
JSONArray jsonArray = new JSONArray(response.body().string());
for(int i=0;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
DataJson data = new DataJson(object.getInt("id"), object.getString("username"), object.getString("email"), object.getString("tanggal_lahir"));
dataku.add(data);
}
}
catch (IOException e){
e.printStackTrace();
}
catch (JSONException e){
System.out.println("Akhir Json File");
}
return null;
}
@Override
protected void onPostExecute(Void Avoid){
adapter.notifyDataSetChanged();
}
};
task.execute();
}
这是我的日志:
01-22 20:47:42.830 10352-10366/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: ptguwatirtasejahtera.utraposmobile, PID: 10352
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at ptguwatirtasejahtera.utraposmobile.FragmentUtama$2.doInBackground(FragmentUtama.java:156)
at ptguwatirtasejahtera.utraposmobile.FragmentUtama$2.doInBackground(FragmentUtama.java:147)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
什么是错的,有什么想法吗?我现在是stackoverflow的新手。 谢谢你的帮助
答案 0 :(得分:0)
我看到可能抛出ArrayIndexOutOfBoundsException的两个可能源。 首先是适配器,我看到dataku是一个没有Elements的ArrayList。适配器可以处理空列表吗? 第二个可以找到LastCompletelyVisibleItemPosition()返回-1,因为如果列表的大小为0,则尝试在dataku.get(dataku.size() - 1)中访问位置-1。