Today I just started learning Retrofit2 and immediately ran into problems:

  • Everywhere in the annotations @POST and @GET you need to register the parameters. But I have test.ru/mobile-api.php . And there are data entry forms. Empty POST is sent, error gives

  • I 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?)

  • one
    Try a query on the field to replace - YuriySPb
  • Added before @POST @FormUrlEncoded earned)) Thank you. - DevOma
  • What about a 2-point. In order to manually, in onResponse the second parameter, it already comes to the List, I want to insert myself into the List - DevOma
  • Not really understood the second part. Maybe you need to replace the type of the received object with Responce<ResponceBody> - Yuriy SPb
  • I mean, before that, json got the String type and manually everything was properly packaged or parsil, well, here it is. And as you can see, I get a ready-made List - DevOma

0