java.lang.IllegalArgumentException:URL查询字符串“ text = {userInput}&key = {apiKey}”必须没有替换块。对于动态查询参数,请使用@Query。
这是我的代码:
public interface Service {
@GET("/check.php?text={userInput}&key={apiKey}")
Call<List<Errors>> readErrors(@Query("userInput") String userInput,
@Query("apiKey") String apiKey);
}
我的通话请求:
public void loadJson(){
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("https://api.textgears.com")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
Service serviceAPI = retrofit.create(Service.class);
Call<List<Errors>> loadErrorsCall = serviceAPI.readErrors(userInput, apiKey);
loadErrorsCall.enqueue(new Callback<List<Errors>>() {
@Override
public void onResponse(Call<List<Errors>> call, Response<List<Errors>> response) {
errors = new ArrayList<>(response.body());
Log.i("ORIG. ARRAY SIZE", String.valueOf(errors.size()));
if (errors != null){
for (int i = 0; i < 5; i++){
errorArrayList.add(errors.get(i));
}
Log.i("NEW ARRAY SIZE", String.valueOf(errorArrayList.size()));
}
mErrorsRecyclerView.setItemAnimator(new DefaultItemAnimator());
mErrorsRecyclerView.setAdapter(new ResultAdapter(getContext(), errorArrayList));
}
@Override
public void onFailure(Call<List<Errors>> call, Throwable t) {
Log.i("Error: ", t.getMessage());
}
});
}
我的问题应该怎么解决?
编辑: 我已经解决了我的问题,但是我的另一个问题是我的回收站视图没有显示。说实际上没有适配器,但找不到适配器。这是我的代码,Adapter类和fragment类。
适配器类:
public class ResultAdapter extends RecyclerView.Adapter<ResultAdapter.ResultViewHolder>{
Context mContext;
List<Photo> photoList = new ArrayList<>();
List<Errors> errorsList = new ArrayList<>();
public ResultAdapter (Context mContext, List<Errors> errorsList){
this.errorsList = errorsList;
this.mContext = mContext;
}
@Override
public ResultViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.result_card, viewGroup, false);
return new ResultViewHolder(view);
}
@Override
public void onBindViewHolder(ResultViewHolder resultViewHolder, int i) {
Errors errors = errorsList.get(i);
//Log.i("Position: ", i+1 + " Id: " + photos.getId());
resultViewHolder.mNumErrorsTextView.setText(errorsList.size());
resultViewHolder.mIdErrorTextView.setText(errors.getId());
resultViewHolder.mLengthErrorTextView.setText(errors.getLength());
resultViewHolder.mBadErrorTextView.setText(errors.getBad());
}
@Override
public int getItemCount() {
return errorsList.size();
}
public class ResultViewHolder extends RecyclerView.ViewHolder{
@BindView(R.id.NumofErrorsTextView)
TextView mNumErrorsTextView;
@BindView(R.id.ErrorIdTextView)
TextView mIdErrorTextView;
@BindView(R.id.ErrorLengthTextView)
TextView mLengthErrorTextView;
@BindView(R.id.ErrorBadTextView)
TextView mBadErrorTextView;
public ResultViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
片段类:
public class Tab1Fragment_GrammarChecker extends Fragment {
private static final String TAG = "Tab1Fragment";
@BindView(R.id.InputTextEditText)
EditText mInputGrammarEditText;
@BindView(R.id.ErrorsRecyclerView)
RecyclerView mErrorsRecyclerView;
List<Errors> errors = new ArrayList<>();
public ArrayList<Errors> errorArrayList = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1_grammar_checker, container, false);
ButterKnife.bind(this, view);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
mErrorsRecyclerView.setLayoutManager(layoutManager);
loadJson();
return view;
}
@OnClick(R.id.checkGrammarButton)
public void setOnClick(View view){
Toast.makeText(getActivity(), "Check Grammar", Toast.LENGTH_LONG).show();
}
public void loadJson(){
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("https://api.textgears.com")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
Service serviceAPI = retrofit.create(Service.class);
Call<List<Errors>> loadErrorsCall = serviceAPI.readErrors(userInput, apiKey);
loadErrorsCall.enqueue(new Callback<List<Errors>>() {
@Override
public void onResponse(Call<List<Errors>> call, Response<List<Errors>> response) {
errors = new ArrayList<>(response.body());
Log.i("ORIG. ARRAY SIZE", String.valueOf(errors.size()));
if (errors != null){
for (int i = 0; i < 5; i++){
errorArrayList.add(errors.get(i));
}
Log.i("NEW ARRAY SIZE", String.valueOf(errorArrayList.size()));
}
mErrorsRecyclerView.setItemAnimator(new DefaultItemAnimator());
mErrorsRecyclerView.setAdapter(new ResultAdapter(getContext(), errorArrayList));
}
@Override
public void onFailure(Call<List<Errors>> call, Throwable t) {
Log.i("Error: ", t.getMessage());
}
});
}
}
答案 0 :(得分:0)
您的API接口应如下所示:
public interface Service {
@GET("/check.php")
Call<List<Errors>> readErrors(@Query("userInput") String userInput,
@Query("apiKey") String apiKey);
}
所以,用这个替换您的界面。
提示:请在回收者视图中添加布局管理器:
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mErrorsRecyclerView.setLayoutManager(mLayoutManager);