其实我想通过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/