简体   繁体   English

如何使用 rest api 从谷歌驱动器读取 json

[英]How to read json from google drive with rest api

I have created some Json file that contains shop's objects and i want to store it on Google drive and read it with Retrofit.我创建了一些包含商店对象的 Json 文件,我想将其存储在 Google 驱动器上并使用 Retrofit 读取它。

Currently, I can't store it in local memory or in-app.目前,我无法将其存储在本地 memory 或应用内。 Also, there is no server side yet, so it needs to be stored somewhere that Retrofit can access.另外,还没有服务器端,所以它需要存储在 Retrofit 可以访问的地方。 If you have any other ideas, I'd be more than happy to hear.如果您有任何其他想法,我将非常乐意听到。

I make the link public to anyone, and here is my.json file:我将链接公开给任何人,这里是 my.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"
        }
      ]
    }
  ]
}

I've tried the next steps but it's not work for me.我已经尝试了接下来的步骤,但这对我不起作用。

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());
            }
        });

That what i receive in logcat:我在 logcat 中收到的内容:

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

I think there is something wrong with your pojo objects.我认为您的 pojo 对象有问题。 It should be like this according to your response根据你的回复应该是这样的

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;
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM