【问题标题】:I want to fetch data from Mysql database through retrofit2 api我想通过retrofit2 api从Mysql数据库中获取数据
【发布时间】:2018-10-07 16:27:59
【问题描述】:

其实我想通过retrofit2 api从Mysql数据库中获取数据。我正在根据类别 1 或 2 等类别获取数据,但我收到错误 @Field parameters can only be used with form encoding. (parameter #1)for method APIService.savePost

这是我的接口代码:

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 的片段中的代码:

 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 上。

【问题讨论】:

    标签: php android api syntax-error retrofit2


    【解决方案1】:
    @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);
    

    【讨论】:

    • 现在出现此错误....... 使用 JsonReader.setLenient(true) 在第 1 行第 1 列路径 $ 接受格式错误的 JSON
    • 你得到了一个格式错误的 json,如果没有看到它很难说是什么问题。
    • 现在出现此错误....应为 BEGIN_ARRAY 但在第 1 行第 1 列路径 $
    • Retrofit 期待一个 List 但它接收一个字符串。你的 json 应该是 [{"field1":"value"},{"field1":"value"},{"field1":"value"}] 这样 gson 可以解析它。
    猜你喜欢
    • 1970-01-01
    • 2013-02-10
    • 2018-01-18
    • 1970-01-01
    • 2015-07-28
    • 2017-12-07
    • 1970-01-01
    • 2016-10-21
    相关资源
    最近更新 更多