1

I'm developing an android grammar application using Android Studio and I'm using Textgears as an API. Does anyone know how to apply textgears in api? I'm having a problem in displaying the expected output.

Update: I add my code and also I am using Retrofit for accessing for the network call.

Service.class

public interface Service {

     @GET("/check.php?")
     Call<List<Errors>> readErrors(@Query("userInput") String userInput,
                              @Query("apiKey") String apiKey);

}

Fragment.class

public class Tab1Fragment_GrammarChecker extends Fragment {

@BindView(R.id.InputTextEditText)
EditText mInputGrammarEditText;

@BindView(R.id.ErrorsRecyclerView)
RecyclerView mErrorsRecyclerView;

List<Errors> errors = new ArrayList<>();

public ArrayList<Errors> errorArrayList = new ArrayList<>();

public String userInput = "I is an engeneer!";
public String apiKey = ""; //apiKey is confidential

@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(){
   // Service serviceAPI = Client.getClient();
    //Call<List<Errors>> loadErrorsCall = serviceAPI.readErrorsArray();

    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.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);
    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 photoList.size();
    return errorsList.size();
}

public class ResultViewHolder extends RecyclerView.ViewHolder{

    TextView mNumErrorsTextView;
    TextView mIdErrorTextView;
    TextView mLengthErrorTextView;
    TextView mBadErrorTextView;

    public ResultViewHolder(@NonNull View itemView) {
        super(itemView);

        //ButterKnife.bind(this, itemView);
        mNumErrorsTextView = (TextView) itemView.findViewById(R.id.NumofErrorsTextView);
        mIdErrorTextView = (TextView) itemView.findViewById(R.id.ErrorIdTextView);
        mLengthErrorTextView = (TextView) itemView.findViewById(R.id.ErrorLengthTextView);
        mBadErrorTextView = (TextView) itemView.findViewById(R.id.ErrorBadTextView);
    }
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • please show your codes – matio Dec 29 '18 at 13:20
  • @matio already add my codes – Patricia Ortega Dec 29 '18 at 13:49
  • what error do you get? – matio Dec 29 '18 at 14:30
  • @matio it displays no output. my expected output should display the suggestions of grammar or spelling in the recycler view. I tried this https://jsonplaceholder.typicode.com/photos as my baseurl, and it works well. but when i tried the textgears, no output is given to me, – Patricia Ortega Dec 29 '18 at 14:47
  • share your logcat here please – matio Dec 29 '18 at 15:09
  • @matio this one is always displaying on my logcat E/RecyclerView: No adapter attached; skipping layout I/Error:: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ – Patricia Ortega Dec 29 '18 at 15:20
  • The problem is you call backend network operations in the main thread. Look at this topic https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout – matio Dec 29 '18 at 15:30
  • Also look at the API documentation, it seems your return result from the API is not correct. Check if you call the correct url path to the API – matio Dec 29 '18 at 15:31
  • @matio i followed the documentation. you might want to see the documentation: here it is, https://textgears.com/api/ – Patricia Ortega Dec 29 '18 at 15:46

0 Answers0