gpt4 book ai didi

java - JSON 请求返回 null

转载 作者:行者123 更新时间:2023-11-29 07:09:40 26 4
gpt4 key购买 nike

我在 Android 上使用 Volley 库,但是当我创建 JSONArrayRequest 时,它会自动返回 null。但如果我继续调试,在第二次尝试中它会填充数组。这是我的代码:

BackgroundTask.java

public class BackgroundTask{

Context context;
ArrayList<Dua> arrayList = new ArrayList<>();
String json_url = "http://www.burakakyalcin.site/getDua.php";

public BackgroundTask(Context context){
this.context = context;
}

public ArrayList<Dua> getList(){
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, json_url, (String)null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
int count = 0;
while(count<response.length()){
try {
JSONObject jsonObject = response.getJSONObject(count);
Dua dua = new Dua(jsonObject.getString("DuaId"),jsonObject.getString("DuaSender"),jsonObject.getString("DuaHeader"),
jsonObject.getString("DuaText"),jsonObject.getString("DuaLike"));
arrayList.add(dua);
count++;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
return arrayList;
}

}

MyFragment.java

public class MyFragment extends Fragment{

RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Dua> arrayList = new ArrayList<>();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setHasFixedSize(true);
BackgroundTask backgroundTask = new BackgroundTask(getActivity().getApplicationContext());
arrayList = backgroundTask.getList();
adapter = new RecyclerAdapter(arrayList);
recyclerView.setAdapter(adapter);
return rootView;
}


}

RecyclerAdapter.java

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

ArrayList<Dua> arrayList = new ArrayList<>();

public RecyclerAdapter(ArrayList<Dua> arrayList){
this.arrayList = arrayList;
}


public void setArrayList (ArrayList<Dua> arrayList){
this.arrayList=arrayList;
notifyItemRangeChanged(0, arrayList.size());
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.DuaSender.setText(arrayList.get(position).getDuaSender());
holder.DuaHeader.setText(arrayList.get(position).getDuaHeader());
holder.DuaLike.setText(arrayList.get(position).getDuaLike());
}

@Override
public int getItemCount() {
return arrayList.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder{

TextView DuaHeader, DuaSender, DuaLike;

public MyViewHolder(View itemView) {
super(itemView);
DuaHeader = (TextView) itemView.findViewById(R.id.duaHeaderRec);
DuaSender = (TextView) itemView.findViewById(R.id.duaSenderRec);
DuaLike = (TextView) itemView.findViewById(R.id.duaLikeRec);
}
}
}

最佳答案

JsonArrayRequest 是异步的。

当你打电话时

arrayList = backgroundTask.getList();

首先你会得到空列表:

ArrayList<Dua> arrayList = new ArrayList<>();

只有在 HTTP 调用成功后,它才会被填充。所以,基本上你需要等待结果才能使用它。

public class BackgroundTask{

Context context;
Fragment fragment;
ArrayList<Dua> arrayList = new ArrayList<>();
String json_url = "http://www.burakakyalcin.site/getDua.php";

public BackgroundTask(Context context, MyFragment fragment){
this.context = context;
this.fragment = fragment;
}

public void getList(){
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, json_url, (String)null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Map the JSON as you did before

// Call your fragment
fragment.setList(arrayList);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});

MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
}

在您的 fragment 中,您需要一个 setter 方法。只有当它被调用时,你才应该渲染列表:

public class MyFragment extends Fragment{
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Dua> arrayList = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);


recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setHasFixedSize(true);

BackgroundTask backgroundTask = new BackgroundTask(getActivity().getApplicationContext(), this);


return rootView;
}

public void setList(List<Dua> list) {
adapter = new RecyclerAdapter(list);
recyclerView.setAdapter(adapter);
}

}

关于java - JSON 请求返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40328215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com