首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用rest api从google drive读取json

如何使用rest api从google drive读取json
EN

Stack Overflow用户
提问于 2020-07-06 01:19:50
回答 1查看 103关注 0票数 0

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

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

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

代码语言:javascript
运行
复制
{
  "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"
        }
      ]
    }
  ]
}

我已经尝试了下一步,但对我不起作用。

代码语言:javascript
运行
复制
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中接收到的内容:

代码语言:javascript
运行
复制
D/myDebug: onResponse: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $
EN

回答 1

Stack Overflow用户

发布于 2020-07-06 01:35:32

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

代码语言:javascript
运行
复制
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;
}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62743987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档