php - 我想通过 retrofit2 api 从 Mysql 数据库中获取数据

标签 php android api syntax-error retrofit2

其实我想通过retrofit2 api从Mysql数据库中获取数据。我正在根据类别 1 或类别 2 等类别获取数据,但出现错误 @Field parameters can only be used with form encoding。 (参数 #1)用于方法 APIService.savePost

这是我的接口(interface)代码:

public interface APIService {

    @GET("fetchtext.php")
    Call<List<DataStored>> savePost(@Field("catId") String catId);
    @GET("testing.php")
    Call<List<DataStored>> searchcategory(@Field("catId") String catId,
            @Field("SubCatego") String SubCatego);
}

这是我的 ApiUtils 类:

    public class ApiUtils {

  private ApiUtils() {}

    public static final String BASE_URL = "http://192.168.9.10/";

    public static APIService getAPIService() {

        return RetrofitClient.getClient(BASE_URL).create(APIService.class);
    }
}

RetrofitClient 类:

public class RetrofitClient {
    private static Retrofit retrofit = null;
    public static Retrofit getClient(String baseUrl) {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }}

我正在调用 RetroApi 创建的 Fragment 中的代码:

 mAPIService = ApiUtils.getAPIService();
     mAPIService.savePost(category).enqueue(new Callback<List<DataStored>>() {
            @Override
            public void onResponse(Call<List<DataStored>> call, Response<List<DataStored>> response) {
                dataStored=  response.body();
                myRecyclerAdapter.addItems(dataStored);
            }

            @Override
            public void onFailure(Call<List<DataStored>> call, Throwable t) {

            }
        });

请告诉我如何将类别发送到数据库进行比较 数据库中可用的类别数据然后将数据转换为 Json 并在 android 中发送回以显示在 android 中的 RecyclerView 上。

最佳答案

@GET("fetchtext.php")
Call<List<DataStored>> savePost(@Field("catId") String catId);

@Field 用于发送 POST 数据。要获取数据,您应该使用@Query

@GET("fetchtext.php")
Call<List<DataStored>> savePost(@Query("catId") String catId);

关于php - 我想通过 retrofit2 api 从 Mysql 数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50052014/

相关文章:

api - Shopify API : Get Orders In Shopify by Date

php - 使用 php 在 rhel 4 服务器上将 .doc 和 .xls 文件转换为 html

安卓磨损 : Peek card background under clocks

javascript - API正在返回函数

android - 以编程方式设置后获取奇怪的字体大小

android - NotificationCompat.setStyle() 无法获取 Notification.MediaStyle

javascript - 有什么方法可以防止 Stackblitz 中的共享、 fork 选项

php - MySQL 范围日期从第一个日期结果到 x 天

php - 用 PHP 从本地服务器发送电子邮件

php - 使用 JQuery Ajax 通过 HTTPS 发送时未收到 $_POST 变量