【问题标题】:How to read json from google drive with rest api如何使用rest api从谷歌驱动器读取json
【发布时间】:2020-07-05 17:19:50
【问题描述】:

我创建了一些包含商店对象的 Json 文件,我想将它存储在 Google 驱动器上并使用 Retrofit 读取它。

目前,我无法将其存储在本地内存或应用内。 此外,还没有服务器端,所以它需要存储在 Retrofit 可以访问的地方。 如果您有任何其他想法,我很乐意听到。

我将链接公开给任何人,这是我的 .json 文件:

{
  "shop": [
    {
      "shopName": "Renuar",
      "shopID": "1000",
      "isPaid": "false",
      "branches": [
        {
          "branchName": "Branch 1",
          "branchPhone": "039599559",
          "openingTime": "09:00",
          "closingTime": "21:00",
          "branchLat": "32.000",
          "branchLon": "35.000",
          "branchAddressNote": "Grand Canyon"
        }
      ]
    },
    {
      "shopName": "Castro",
      "shopID": "1000",
      "isPaid": "false",
      "branches": [
        {
          "branchName": "Branch 1",
          "branchPhone": "039599559",
          "openingTime": "09:00",
          "closingTime": "21:00",
          "branchLat": "32.000",
          "branchLon": "35.000",
          "branchAddressNote": "Grand Canyon"
        }
      ]
    }
  ]
}

我已经尝试了接下来的步骤,但它不适合我。

public interface ApiService {
        @GET("file/d/1-lsBIzI7Y5uCg8bG_531o49Dcu6E2RdH/view?usp=sharing")
        Call<ShopsResponse> getAllShops();
    }

    public static class RetrofitInstance{
        public static Retrofit retrofit = null;
        private static final String BASE_URL = "https://drive.google.com/";

        public static ApiService getApiService(){
            if (retrofit == null){

                Gson gson = new GsonBuilder()
                        .setLenient()
                        .create();

                retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create(gson))
                        .build();
            }
            return retrofit.create(ApiService.class);
        }

    }

ApiService apiService = RetrofitInstance.getApiService();
        apiService.getAllShops().enqueue(new Callback<ShopsResponse>() {
            @Override
            public void onResponse(Call<ShopsResponse> call, Response<ShopsResponse> response) {
                ShopsResponse response1 = response.body();
                Log.d(TAG, "onResponse: "+response1.getShop().size());
            }

            @Override
            public void onFailure(Call<ShopsResponse> call, Throwable t) {
                Log.d(TAG, "onResponse: "+t.getMessage());
            }
        });

我在 logcat 中收到的内容:

D/myDebug: onResponse: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $

【问题讨论】:

    标签: java android json retrofit


    【解决方案1】:

    我认为你的 pojo 对象有问题。根据你的回复应该是这样的

    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Branch {
    
    @SerializedName("branchName")
    @Expose
    private String branchName;
    @SerializedName("branchPhone")
    @Expose
    private String branchPhone;
    @SerializedName("openingTime")
    @Expose
    private String openingTime;
    @SerializedName("closingTime")
    @Expose
    private String closingTime;
    @SerializedName("branchLat")
    @Expose
    private String branchLat;
    @SerializedName("branchLon")
    @Expose
    private String branchLon;
    @SerializedName("branchAddressNote")
    @Expose
    private String branchAddressNote;
    
    public String getBranchName() {
    return branchName;
    }
    
    public void setBranchName(String branchName) {
    this.branchName = branchName;
    }
    
    public String getBranchPhone() {
    return branchPhone;
    }
    
    public void setBranchPhone(String branchPhone) {
    this.branchPhone = branchPhone;
    }
    
    public String getOpeningTime() {
    return openingTime;
    }
    
    public void setOpeningTime(String openingTime) {
    this.openingTime = openingTime;
    }
    
    public String getClosingTime() {
    return closingTime;
    }
    
    public void setClosingTime(String closingTime) {
    this.closingTime = closingTime;
    }
    
    public String getBranchLat() {
    return branchLat;
    }
    
    public void setBranchLat(String branchLat) {
    this.branchLat = branchLat;
    }
    
    public String getBranchLon() {
    return branchLon;
    }
    
    public void setBranchLon(String branchLon) {
    this.branchLon = branchLon;
    }
    
    public String getBranchAddressNote() {
    return branchAddressNote;
    }
    
    public void setBranchAddressNote(String branchAddressNote) {
    this.branchAddressNote = branchAddressNote;
    }
    
    }
    
    
    
    
    package com.example;
    
    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Example {
    
    @SerializedName("shop")
    @Expose
    private List<Shop> shop = null;
    
    public List<Shop> getShop() {
    return shop;
    }
    
    public void setShop(List<Shop> shop) {
    this.shop = shop;
    }
    
    }
    
    
    
    
    package com.example;
    
    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Shop {
    
    @SerializedName("shopName")
    @Expose
    private String shopName;
    @SerializedName("shopID")
    @Expose
    private String shopID;
    @SerializedName("isPaid")
    @Expose
    private String isPaid;
    @SerializedName("branches")
    @Expose
    private List<Branch> branches = null;
    
    public String getShopName() {
    return shopName;
    }
    
    public void setShopName(String shopName) {
    this.shopName = shopName;
    }
    
    public String getShopID() {
    return shopID;
    }
    
    public void setShopID(String shopID) {
    this.shopID = shopID;
    }
    
    public String getIsPaid() {
    return isPaid;
    }
    
    public void setIsPaid(String isPaid) {
    this.isPaid = isPaid;
    }
    
    public List<Branch> getBranches() {
    return branches;
    }
    
    public void setBranches(List<Branch> branches) {
    this.branches = branches;
    }
    
    }
    

    【讨论】:

    • 我有相同的 pojo 课程。
    猜你喜欢
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 2020-05-01
    • 2012-11-04
    • 1970-01-01
    • 2015-10-30
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多