Patricia Ortega
Patricia Ortega

Reputation: 81

Android Retrofit: URL query string "text={userInput}&key={apiKey}" must not have replace block. For dynamic query parameters use @Query

java.lang.IllegalArgumentException: URL query string "text={userInput}&key={apiKey}" must not have replace block. For dynamic query parameters use @Query.

Here is my code:

public interface Service {

@GET("/check.php?text={userInput}&key={apiKey}")
Call<List<Errors>> readErrors(@Query("userInput") String userInput,
                              @Query("apiKey") String apiKey);
 }

And my call request:

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());
        }
    });
}

What should be the solution to my problem?

Edit: I already fixed my problem but my other problem is that my recycler view is not displaying. Saying that the adapter is not found, when in fact I have an adapter. Here are my codes, the Adapter class and the fragment class.

Adapter class:

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);
    }
}

Fragment class:

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());
        }
    });
}

}

Upvotes: 0

Views: 1087

Answers (1)

Ankit Mehta
Ankit Mehta

Reputation: 4269

Your API Interface should be like following:

public interface Service {
@GET("/check.php")
Call<List<Errors>> readErrors(@Query("userInput") String userInput,
                              @Query("apiKey") String apiKey);
 }

So, replace your interface with this one.

A TIP: Please add a layout manager to the recycler view:

LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mErrorsRecyclerView.setLayoutManager(mLayoutManager);

Upvotes: 1

Related Questions