我正在尝试使用Google表格API写入Google表格文档。我正在使用快速入门中提供的示例代码,但是却遇到了空指针异常。错误发生在此行
Sheets.Spreadsheets.Values.Update request = sheetsService.spreadsheets().values()
.update(spreadsheetID, range, valRange);
我相信这是因为这行返回一个空对象
Sheets sheetsService = createSheetsService();
下面是createSheetsService()方法的代码。
public static Sheets createSheetsService() throws IOException {
try {
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"
// "https://www.googleapis.com/auth/cloud-platform"
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("PathToProject"))
.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
return new Sheets.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Test App")
.build();
} catch (GeneralSecurityException e) {
Log.e(TAG, "Security Exception " + e.getMessage());
return null;
}
}
我很确定这是由于末尾的catch语句而返回null。但是,如果我尝试抛出GeneralSecurityException而不是使用try catch,则会出现此错误
Unhandled exception: java.security.GeneralSecurityException
我真的很茫然,非常感谢您的帮助。 这是我的MainActivity.java文件中的其余代码
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText nameText = (EditText) findViewById(R.id.editText);
Button submit = (Button) findViewById(R.id.button);
final String name = nameText.getText().toString();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
write(name);
}
});
}
public static void write(String args) {
try {
String spreadsheetID = "mySpreadSheetID";
String valueInputOption = "RAW";
BatchUpdateValuesRequest requestBody = new BatchUpdateValuesRequest();
List<ValueRange> data = new ArrayList<>();
//List<List<Object>> vals = Arrays.asList(Arrays.asList((Object) "hello world"));
ValueRange valRange = new ValueRange();
valRange.setValues(
Arrays.asList(
Arrays.asList((Object) ""),
Arrays.asList((Object) "Row 2 Cell 1", (Object) "Row 2 Cell 2")));
String range = "A2";
valRange.setRange(range);
Sheets sheetsService = createSheetsService();
Sheets.Spreadsheets.Values.Update request = sheetsService.spreadsheets().values()
.update(spreadsheetID, range, valRange);
request.setValueInputOption(valueInputOption);
UpdateValuesResponse response = request.execute();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Sheets createSheetsService() throws IOException {
try {
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"
// "https://www.googleapis.com/auth/cloud-platform"
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("PathToProject"))
.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
return new Sheets.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("TestApp")
.build();
} catch (GeneralSecurityException e) {
Log.e(TAG, "Security Exception " + e.getMessage());
return null;
}
}
}