/公共类MainActivity扩展了AppCompatActivity {
private RecyclerView recyclerview;
private RecyclerView.LayoutManager layoutmanager;
private List<Potrawy> potrawy;
private Adapter adapter;
private ApiInterface apiinterface;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress);
recyclerview = findViewById(R.id.recycler);
layoutmanager = new LinearLayoutManager(this);
recyclerview.setLayoutManager(layoutmanager);
recyclerview.setHasFixedSize(true);
fetchPotrawy(""); // without keyword
}
public void fetchPotrawy(String key){
apiinterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<List<Potrawy>> call = apiinterface.getPotrawy(key);
call.enqueue(new Callback<List<Potrawy>>() {
@Override
public void onResponse(Call<List<Potrawy>> call, Response<List<Potrawy>> response) {
progressBar.setVisibility(View.GONE);
potrawy = response.body();
adapter = new Adapter(potrawy, MainActivity.this);
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<Potrawy>> call, Throwable t) {
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "Error on: " + t.toString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName())
);
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
fetchPotrawy(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
fetchPotrawy(newText);
return false;
}
});
return true;
}
/ 公共课Potrawy {
@SerializedName("id") private int id;
@SerializedName("nazwa") private String nazwa;
@SerializedName("keywords") private String keywords;
public int getId() {
return id;
}
public String getNazwa() {
return nazwa;
}
public String getKeywords() {
return keywords;
}
}
/ 公共接口ApiInterface {
@GET("getPotrawy.php")
Call<List<Potrawy>> getPotrawy (@Query("key") String keyword);
}
/ 公共类ApiClient {
public static final String BASE_URL = "http://localhost/inz/";
public static Retrofit retrofit;
public static Retrofit getApiClient(){
if(retrofit==null)
{
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
/公共类Adapter扩展了RecyclerView.Adapter {
private List<Potrawy> potrawy;
private Context context;
public Adapter(List<Potrawy> potrawy, Context context) {
this.potrawy = potrawy;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder( MyViewHolder holder, int position) {
holder.nazwa.setText(potrawy.get(position).getNazwa());
}
@Override
public int getItemCount() {
return potrawy.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView nazwa;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
nazwa = itemView.findViewById(R.id.nazwa);
}
}
我该如何解决?没有一个stackoverflow线程可以帮助我。 问题名称在主题中 java.lang.illegalstateexception预期为begin_array,但在第1行第1列路径$
处为字符串