java如果我的数据在数组列表中,如何从网络获取响应
我想从网络上得到响应,这是一个数组列表,并将该数组列表发送到下一个活动,以便我可以在我的recyclerView中显示它
public void sendPost(){
Call<List<SearchModel>> call = mAPIService.sendSearch("1","2");
call.enqueue(new Callback<List<SearchModel>>() {
@Override
public void onResponse(Call<List<SearchModel>> call, retrofit2.Response<List<SearchModel>> response) {
Toast.makeText(getApplicationContext(), " Responce " +response.body(), Toast.LENGTH_SHORT).show();
if (response.isSuccessful()){
SearchModel searchResponse = response.body().get(0);
assert searchResponse != null;
searchModelList.add(searchResponse);
Intent intent1 = new Intent(getApplicationContext(),ResultsSearch.class);
Bundle bundle = new Bundle();
bundle.putSerializable("mylist",searchModelList);
intent1.putExtras(bundle);
startActivity(intent1);
}else {
Toast.makeText(getApplicationContext(),"Something is error",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<List<SearchModel>> call, Throwable t) {
Log.e("msg","Failed "+t);
Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
而且,当我通过在模型中存储该值来发送单个数据时,我的应用程序崩溃。如何解决这些错误。 我将在下一个活动中获得以下值:
public class ResultsSearch extends AppCompatActivity {
Intent mIntent;
List vehicleResponse;
Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results_search);
bundle = this.getIntent().getExtras();
init();
}
private void init(){
vehicleResponse = bundle.getParcelableArrayList("mylist");
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
SearchAdapter searchAdapter = new SearchAdapter(vehicleResponse,getApplicationContext());
recyclerView.setAdapter(searchAdapter);
}
}
# 1 楼答案
确保SearchModel是可打包的。 但正确的方法是在Bundle中传递search的参数,并对ResultsSearch进行API调用