Today I just started learning Retrofit2 and immediately ran into problems:
Everywhere in the annotations
@POSTand@GETyou need to register the parameters. But I have test.ru/mobile-api.php . And there are data entry forms. Empty POST is sent, error givesI need to manually pack a JSON answer.
Until today, I used the DefaultHttpClient and sent it like this:
List<NameValuePair> valuePairs = new ArrayList<>(); valuePairs.add(new BasicNameValuePair("login", Application.Login)); valuePairs.add(new BasicNameValuePair("limit", "20")); valuePairs.add(new BasicNameValuePair("f", "get_all_vacancies")); valuePairs.add(new BasicNameValuePair("page", String.valueOf(vacancyPage))); HttpResponse httpresponse = HttpUtils.getInstance().post(Application.VACANCY_API, valuePairs); final String response = HttpUtils.httpResponse2String(httpresponse); if (response != null) { }
It worked and could check the response .
But now I am making a request using Retrofit2
In the interface
@POST("/mobile-api.php") Call<List<Model>> getAllVacancies(@Query("login") String login, @Query("limit") String limit, @Query("f") String query_name, @Query("page") String page); In App Class
retrofit = new Retrofit.Builder() .baseUrl("http://test.ru") .addConverterFactory(GsonConverterFactory.create()) .build(); In MainActivity
App.getApi() .getAllVacancies(App.Login, App.Limit, "get_all_vacancies", "1") .enqueue(new Callback<List<Model>>() { @Override public void onResponse(Call<List<Model>> call, Response<List<Model>> response) { Toast.makeText(MainActivity.this, "Успех", Toast.LENGTH_SHORT).show(); modelList.addAll(response.body()); recyclerView.getAdapter().notifyDataSetChanged(); } @Override public void onFailure(Call<List<Model>> call, Throwable t) { Toast.makeText(MainActivity.this, "Произошла ошибка при соединении", Toast.LENGTH_SHORT).show(); } }); I can not do debugging, it immediately onFailure() to onFailure()
Question: What am I doing wrong?)
@POST@FormUrlEncodedearned)) Thank you. - DevOmaResponce<ResponceBody>- Yuriy SPb ♦