如何将数据从okhttp和Asynctask放入RecyclerView



我通过okhttp从服务器获取了一些Json数据,我想在 使用自定义适配器在片段内回收视图,并收到以下错误。

当我运行这段代码时,logcat 告诉我以下消息:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference

我在日志猫中打印列表,它显示空。 但是我可以从有人帮助我的 IE.Do 获得一些 json 数据,请?

public class customer extends AppCompatActivity {
private static final String TAG = "MyDebug______ " ;
private RecyclerView cutomerRecyclerView;
private customerAdapter mAdapter;
private ArrayList<custModal> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
Toolbar toolbar = (Toolbar)findViewById(R.id.custom_toolbar);
setSupportActionBar(toolbar);
new getHttpData().execute();
Log.d(TAG, "the list is :" +list);
mAdapter = new customerAdapter(this,list);
cutomerRecyclerView = (RecyclerView)findViewById(R.id.customer_list);
cutomerRecyclerView.setLayoutManager(new LinearLayoutManager(this));
cutomerRecyclerView.setAdapter(mAdapter);
cutomerRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration(){
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state){
super.onDraw(c,parent,state);
c.drawColor(R.color.colorDevider);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.offset(0,1);
}
});
}

public class customerAdapter extends RecyclerView.Adapter<customerAdapter.customerViewHolder>{
private Context mcontext;
private ArrayList<custModal> mlist;
// private LayoutInflater inflater;
class customerViewHolder extends RecyclerView.ViewHolder{
private TextView name,mem,cust_id;
public customerViewHolder(View view){
super(view);
cust_id = (TextView )view.findViewById(R.id.cust_id);
name = (TextView)view.findViewById(R.id.cust_name);
mem = (TextView)view.findViewById(R.id.cust_mem);
}
}
public customerAdapter(Context context,ArrayList<custModal> list){
this.mcontext = context;
this.mlist = list;
}
@Override
public customerViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
customerViewHolder holder = new customerViewHolder(LayoutInflater.from(
customer.this).inflate(R.layout.customer_item,parent,false));
return holder;
}
@Override
public void onBindViewHolder(customerViewHolder holder,int position){
holder.cust_id.setText(mlist.get(position).getId());
holder.name.setText(mlist.get(position).getName());
holder.mem.setText(mlist.get(position).getMobiphone());
}
@Override
public int getItemCount(){ return mlist.size();}

}

类 custModal {

private String id;
private String name;
private String gender;
private String mobiphone;
private String creat_time;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getGender() {
return gender;
}
public void setMobiphone(String mobiphone) {
this.mobiphone = mobiphone;
}
public String getMobiphone() {
return mobiphone;
}
public void setCreat_time(String creat_time) {
this.creat_time = creat_time;
}
public String getCreat_time() {
return creat_time;
}
}

public class getHttpData extends AsyncTask<String,Integer,ArrayList<custModal> >{
//private ArrayList<custModal> custlist;
@Override
// protected void onPreExecute(){}
@Override
protected ArrayList<custModal> doInBackground(String... params){
String url = "http://aaaa.bbbbb.cn/index.php/mobi/customer/app_getlist";
try{
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
Log.d(TAG, "doInBackground: "+ responseData);
if(responseData != null){
//return getCustFromJson(responseData);
Gson gson = new Gson();
ArrayList<custModal> custlist = gson.fromJson(responseData,
new TypeToken<List<custModal>>() {}.getType()
);
return custlist;
}else{
return null;
}
}catch (IOException I){
I.printStackTrace();
}
return null;
}
@Override
protected  void onPostExecute(ArrayList<custModal> custlist){
if(!custlist.isEmpty()){
super.onPostExecute(custlist);
list = custlist;
mAdapter.notifyDataSetChanged();
}
}
}

}

确保初始化适配器,适配器数组列表并将适配器连接到回收器onCreate然后,您必须在需要http调用的位置添加新的yourAsyncTask((.execute。 在您的onBackground将项目添加到数组列表中,postExecute只需调用通知数据更改方法

当您通过适配器构造函数传递列表时,列表处于 null 状态,因此列表上的任何操作都将返回 null。

您可以向okhttp请求添加回调并在响应上构造适配器吗?一种或另一种方式,但您需要先更新列表,然后才能对其执行任何操作。

我将以下代码放入方法onPostExecute,它起作用了。但我认为这不是一个正确的解决方案,就像这样:

protected  void onPostExecute(ArrayList<custModal> custlist){
Log.d(TAG, "onPostExecute(Result result) called");
Log.d(TAG, "onPostExecute: "+custlist);
mAdapter = new customerAdapter(customer.this,custlist);
cutomerRecyclerView = (RecyclerView)findViewById(R.id.customer_list);
cutomerRecyclerView.setLayoutManager(new LinearLayoutManager(customer.this));
cutomerRecyclerView.setAdapter(mAdapter);
}

相关内容

  • 没有找到相关文章

最新更新