简体   繁体   中英

Android studio: Read data from Google spreadsheet using retrofit2 (GET)

Right now I'm able to post data using a method from "Blundell Android Developer Tutorials and Blog", but I need to read data from different entries.

This is the interface for post:

public interface WebServiceSpreadsheet {

    @POST("1FAIpQLSfKGadkannwHzBhQDhJ9VBidB9oUhX-0mxI6vnyNCzo5G_YSg/formResponse")
    @FormUrlEncoded
    Call<Void> SpreadshettData(
        @Field("entry.1286709617") String ActuatorsApp
    );

}

And the Java code to post:

public class ActActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act);
        Retrofit retrofit = new Retrofit.Builder().baseUrl(
            "https://docs.google.com/a/acorntechmx.com/forms/d/e/")
            .build();

        final WebServiceSpreadsheet spreadsheetWebService = 
            retrofit.create(WebServiceSpreadsheet.class);


        findViewById(R.id.btnFan1).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String FanON1 = "Fan ON Hour";

                    Call<Void> SpreadsheetData = 
                        spreadsheetWebService.SpreadshettData(FanON1);
                    SpreadsheetData.enqueue(callCallback);
                }
            }
    );

What should I do to read data with the GET method from retrofit2 ?

To read data from the Google Sheets API with the GET method from retrofit2 you should add some methods to your WebServiceSpreadsheet interface:

Returns the spreadsheet at the given ID:

  @GET("/v4/spreadsheets/1FAIpQLSfKGadkannwHzBhQDhJ9VBidB9oUhX-0mxI6vnyNCzo5G_YSg")
  Call<Spreadsheet> get(@Path("spreadsheetId") String spreadsheetId);

After that you may call the method like:

Call<Spreadsheet> spreadsheet = spreadsheetWebService.get(“1FAIpQLSfKGadkannwHzBhQDhJ9VBidB9oUhX-0mxI6vnyNCzo5G_YSg”);

You may use Google Sheets API as a reference

To use Google Sheets API in your Android app this is a shortlist to check:

  1. Enable Google Sheets API for your project or create new project and enable API for it.
  2. Generate Release/Debug keystores for your project and get SHA1 key fingerprint.
  3. Obtain API_KEY from Google Cloud Platform console by inserting the package name and SHA1 key fingerprint from previous step to your project.
  4. Add these to your build.gradle (Module app):

    implementation('com.google.apis:google-api-services-sheets:v4-rev525-1.23.0') { exclude group: 'org.apache.httpcomponents' }

Here is an interface that should work for read-only publicly visible sheets:

public interface SheetsApi {

  @GET("/v4/spreadsheets/{spreadsheetId}/values/{range}?majorDimension=ROWS")
  Call<SheetsValuesResponse> getValues(
      @Path("spreadsheetId") String sheetId,
      @Path("range") String range,
      @Query("key") String apiKey);

  @GET("/v4/spreadsheets/{spreadsheetId}")
  Call<GetSheetResponse> getSheets(
      @Path("spreadsheetId") String sheetId, @Query("key") String apiKey);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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