我正在开发一款Android应用,它可以从谷歌电子表格中获取信息并输入列表视图。但是,我无法为此提出解决方案。 任何帮助将不胜感激。 对于电子表格集成,我使用Google表格API。 我知道客户端登录不安全,但这只是一个用于项目目的的测试应用程序:)
以下是我的代码 -
package com.bish.test;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gdata.client.authn.oauth.*;
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.*;
import com.google.gdata.data.batch.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.*;
import java.util.*;
public class MainActivity extends ActionBarActivity {
SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1");
String USERNAME = "cccc@gmail.com";
String PASSWORD = "xxx";
ListView list;
String[] web;
Integer[] imageId;
CustomList adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logIn();
try {
refreshData();
} catch (IOException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
adapter = new CustomList(MainActivity.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void logIn()
{
final ProgressDialog ringProgressDialog = ProgressDialog.show(MainActivity.this, "Please wait ...", "Logging In...", true);
ringProgressDialog.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
service.setUserCredentials(USERNAME, PASSWORD);
Thread.sleep(2200);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "ERROR: Check your Internet Connection!", Toast.LENGTH_LONG).show();
e.printStackTrace();
System.exit(0);
}
ringProgressDialog.dismiss();
}
}).start();
}
public void refreshData() throws AuthenticationException, MalformedURLException, IOException, ServiceException
{
}
}
答案 0 :(得分:0)
我希望这会对你有所帮助。 这段代码:
1)创建一个新的电子表格
2)插入新的工作表
3)在单元格中插入一些值
4)显示单元格中的值
import com.google.gdata.client.spreadsheet.FeedURLFactory;
import com.google.gdata.data.spreadsheet.Cell;
import com.google.gdata.data.spreadsheet.CellEntry;
import com.google.gdata.data.spreadsheet.CellFeed;
import com.google.gdata.data.spreadsheet.CustomElementCollection;
import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.data.spreadsheet.ListFeed;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.data.spreadsheet.WorksheetEntry;
import com.google.gdata.util.ServiceException;
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
final String SCOPE = "https://docs.google.com/feeds/default/private/full";
final String CREDENTIALS = sessionManager.getCredentials().getSelectedAccountName();
// CREATE A NEW GOOGLE SPREADSHEET WITH THE CORRESPONDING NAME
SpreadsheetEntry newEntry = new SpreadsheetEntry();
newEntry.setTitle(new PlainTextConstruct(Constants.FILE_PREFIX + mFileName));
URL url = null;
try {
Log.d(TAG, "CRED: " + CREDENTIALS);
url = new URL (SCOPE + "?xoauth_requestor_id=" + CREDENTIALS);
// INSERT THE FILE
spreadsheetService.insert(url, newEntry);
} catch (MalformedURLException e) {
Log.d(TAG, "MalformedURLException: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "IOException: " + e.getMessage());
} catch (ServiceException e) {
Log.d(TAG, "ServiceException: " + e.getMessage());
} catch (IllegalArgumentException e) {
// I RECEIVE MEDIA NOT SUPPORTED EXCEPTION, BUT THE FILE IS CREATED!!!!
Log.d(TAG, "IllegalArgumentException: " + e.getMessage());
} catch (Exception e) {
Log.d(TAG, "Exception: " + e.getMessage());
}
try {
// GET ALL THE FILES FROM THE USER'S GOOGLE DRIVE
SpreadsheetFeed feed = spreadsheetService.getFeed(
FeedURLFactory.getDefault()
.getSpreadsheetsFeedUrl(),
SpreadsheetFeed.class);
// Creating the list of spreasheets in GDrive
List<com.google.gdata.data.spreadsheet.SpreadsheetEntry> spreadsheets = feed.getEntries();
// parsing trough the feed entries
for (int i = 0; i < spreadsheets.size(); i++) {
com.google.gdata.data.spreadsheet.SpreadsheetEntry e = (com.google.gdata.data.spreadsheet.SpreadsheetEntry) spreadsheets.get(i);
// IF WE LOCATE THE FILE BASED ON THE FILENAME
if( e.getTitle().getPlainText().equals(Constants.FILE_PREFIX + mFileName)) {
Log.d(TAG, "ENTRY: " + e.getTitle().getPlainText());
URL worksheetFeedUrl = e.getWorksheetFeedUrl();
Log.d(TAG, "worksheetFeedUrl: " + worksheetFeedUrl);
// The first time this feed is used to create the new worksheet
WorksheetFeed worksheetFeed = spreadsheetService.getFeed (worksheetFeedUrl, WorksheetFeed.class);
Log.d(TAG, "worksheetFeed OK !");
// Create the second worksheet
WorksheetEntry newWorksheet = new WorksheetEntry(15, 5);
newWorksheet.setTitle(new PlainTextConstruct("Sheet2"));
worksheetFeed.insert(newWorksheet);
// The second time this feed is used to get the worksheets
worksheetFeed = spreadsheetService.getFeed (worksheetFeedUrl, WorksheetFeed.class);
// Get the first worksheet and insert the titles
List <WorksheetEntry> worksheetEntrys = worksheetFeed.getEntries ();
WorksheetEntry sheet1 = worksheetEntrys.get(0);
WorksheetEntry sheet2 = worksheetEntrys.get(1);
URL sheet1CellFeedUrl = sheet1.getCellFeedUrl ();
CellFeed sheet1CellFeed = spreadsheetService.getFeed (sheet1CellFeedUrl, CellFeed.class);
sheet1CellFeed.insert (new CellEntry (1, 1, getResources().getString(R.string.cell_title_name)));
sheet1CellFeed.insert (new CellEntry (1, 2, getResources().getString(R.string.cell_title_description)));
sheet1CellFeed.insert (new CellEntry (3, 2, getResources().getString(R.string.some_string)));
sheet1CellFeed.insert (new CellEntry (13, 2, "=COUNTIF(Sheet1!F2:F,B3)"));
sheet1CellFeed.insert (new CellEntry (14, 2, "=B9 - TODAY()"));
// GET THE CONTENT FROM THE CELLS FROM THE SECOND WORKSHEET
URL sheet2CellFeedUrl = sheet2.getCellFeedUrl ();
CellFeed sheet2CellFeed = spreadsheetService.getFeed (sheet2CellFeedUrl, CellFeed.class);
List<CellEntry> cellEntryList = sheet2CellFeed.getEntries();
if(cellEntryList != null && !cellEntryList.isEmpty()) {
for (CellEntry cellEntry : cellEntryList) {
Cell cell = cellEntry.getCell();
Log.d(TAG, "VALUE:" + cell.getValue());
}
}
break;
}
}
} catch (Exception e) {
Log.d(TAG, "Exception: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
showMessage("Error!");
}
});
}
return null;
}
}.execute();