我真的是Android开发的新手。我想要一个带有内置QR码阅读器的应用,该应用会将读取的数据附加到Google表格中。唯一的问题是我不知道如何将数据发送到电子表格。我可以登录Google,获取QR码,但找不到有关Google Sheet API v4的好教程,所以这就是我向您求助的原因。 (当我调用该意图时,该应用程序会冻结)
我有以下代码:
package com.qr_sheets.qr_sheets;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.model.AppendValuesResponse;
import com.google.api.services.sheets.v4.model.ValueRange;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
public class WriteData extends AppCompatActivity {
String spreadsheetId = "1VV13imEvHIjf8bbooXooWfaXEi3mVkes-bGvbes0tr8";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_data);
String range = "A:C"; // TODO: Update placeholder value.
// How the input data should be interpreted.
String valueInputOption = ""; // TODO: Update placeholder value.
// How the input data should be inserted.
String insertDataOption = ""; // TODO: Update placeholder value.
Object a1 = new Object();
a1 = "TEST Row 1 Column A";
Object b1 = new Object();
b1 = "TEST Row 1 Column B";
Object a2 = new Object();
a2 = "TEST Row 2 Column A";
Object b2 = new Object();
b2 = "TEST Row 2 Column B";
// TODO: Assign values to desired fields of `requestBody`:
ValueRange requestBody = new ValueRange();
requestBody.setValues(Arrays.asList(Arrays.asList(a1, b1), Arrays.asList(a2, b2)));
Sheets sheetsService = null;
try {
sheetsService = createSheetsService();
} catch (IOException e) {
e.printStackTrace();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
Sheets.Spreadsheets.Values.Append request = null;
try {
request = sheetsService.spreadsheets().values().append(spreadsheetId, range, requestBody);
} catch (IOException e) {
e.printStackTrace();
}
request.setValueInputOption(valueInputOption);
request.setInsertDataOption(insertDataOption);
try {
AppendValuesResponse response = request.execute();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Sheets createSheetsService() throws IOException, GeneralSecurityException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// TODO: Change placeholder below to generate authentication credentials. See
// https://developers.google.com/sheets/quickstart/java#step_3_set_up_the_sample
//
// Authorize using one of the following scopes:
// "https://www.googleapis.com/auth/drive"
// "https://www.googleapis.com/auth/drive.file"
// "https://www.googleapis.com/auth/spreadsheets"
GoogleCredential credential = null;
return new Sheets.Builder(httpTransport, jsonFactory, credential).setApplicationName("Google-SheetsSample/0.1").build();
}
}
我在这里发现了很多类似案件的问题,但没有找到相同的情况和解决方案,因此,抱歉。