I have a code that loads all the data about cities. How to pass a string to search for cities containing this string? What request need GET or POST?

Interface

public interface APIService { @FormUrlEncoded @POST("register") Call<Result> createUser( @Field("login") String login, @Field("email") String email, @Field("password") String password, @Field("phone") String phone, @Field("gender") String gender ); @FormUrlEncoded @POST("login") Call<Result> userLogin( @Field("login") String login, @Field("password") String password ); @GET("cities") Call<Cities> getCities(@Query("query")String query); @GET("shops") Call<Shops> getShops(); } 

Code

 public void LoadData(String query){ Retrofit retrofit = new Retrofit.Builder() .baseUrl(APIUrl.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); APIService service = retrofit.create(APIService.class); Call<Cities> call = service.getCities(query); call.enqueue(new Callback<Cities>() { @Override public void onResponse(Call<Cities> call, Response<Cities> response) { adapter = new CityAdapter(response.body().getCities(),getActivity()); recyclerViewCity.setAdapter(adapter); } @Override public void onFailure(Call<Cities> call, Throwable t) { Toast.makeText(getActivity(),t.getMessage(),Toast.LENGTH_SHORT).show(); } }); } 

PHP I use SLIM:

 $app->get('/cities', function (Request $request, Response $response) { if(isTheseParametersAvailable(array('query'))){ $requestData = $request->getParsedBody(); $query = $requestData['query']; $db = new DbOperation(); $cities = $db->getAllCities($query); $response->getBody()->write(json_encode(array("cities" => $cities))); } }); .... function getAllCities($query){ $stmt = $this->con->prepare("SELECT id, region, autonom, area, city, city_2 FROM locality WHERE ((region LIKE ? and autonom is null and area is null and city is null and city_2 is null) or (city LIKE ? and city_2 is null) or(city_2 LIKE ?))"); $stmt-> bind_param("sss", $query, $query, $query); $stmt->execute(); $stmt->bind_result($id, $region, $autonom, $area, $city, $city_2); $cities = array(); while($stmt->fetch()){ $temp = array(); $temp['id'] = $id; $temp['region'] = $region; $temp['autonom'] = $autonom; $temp['area'] = $area; $temp['city'] = $city; $temp['city_2'] = $city_2; array_push($cities, $temp); } return $cities; } 

An empty string is returned, although if I execute the SQL query in the database, it will return a value.

Where is the mistake? Can I incorrectly pass the query string? or an error in the server side?

In PostMan, you get a GET request: http://localhost/Retrofit/public/cities?query="Москва" and it returns {"cities":[]} .

I tried in $app->get('/cities', insert the default value. In the postman issued a GET request: http://localhost/Retrofit/public/cities

Method after change:

 $app->get('/cities', function (Request $request, Response $response) { $requestData = $request->getParsedBody(); $query = 'Москва'; $db = new DbOperation(); $cities = $db->getAllCities($query); $response->getBody()->write(json_encode(array("cities" => $cities))); }); 

Issued the result:

 {"cities":[{"id":12865,"region":"\u041c\u043e\u0441\u043a\u0432\u0430","autonom":null,"area":null,"city":null,"city_2":null}]} 

It means an error, either in the reception of a variable, or in the transmission. Help me find a bug

  • It is necessary to look at the API - is there such an opportunity. If - yes there will be written what request to send. Obviously, this will be GET. In the code given by you, there is no such possibility at all. You can only manually match the lines in the received information. - Yuriy SPb
  • if you are talking about the server part of api, then there I will add how to send a string from the application? - danilshik
  • You will need to add a parameter to the method for the GET request and annotate it with @Query - YuriySPb
  • @YuriSPb look at the modified code. Did I pass the parameter correctly? And by what url to take the parameters in the back end - danilshik
  • Pass correctly. Error somewhere in reception. Check what you get on the server. Maybe the parameter may not be drawn out or not in the same encoding - YuriiSPb

0