2016-08-23 90 views
0

我正在为使用android studio的应用程序编写代码,至此我能够从Google电子表格中检索数据。 我用下面的代码:无法使用Android Studio更新Google电子表格API的行。

package com.example.tolotrasamuel.aluoneapp; 

import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.List; 

import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.transform.TransformerException; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.util.Log; 

import com.google.gdata.client.spreadsheet.SpreadsheetService; 
import com.google.gdata.data.spreadsheet.ListEntry; 
import com.google.gdata.data.spreadsheet.ListFeed; 
import com.google.gdata.data.spreadsheet.WorksheetEntry; 
import com.google.gdata.data.spreadsheet.WorksheetFeed; 
import com.google.gdata.util.AuthenticationException; 
import com.google.gdata.util.ServiceException; 

public class GetDataAsyncTask extends AsyncTask<Activity, Void, Void> { 

    Activity myActivity; 
    @Override 
    protected Void doInBackground(Activity... arg0) { 
     // TODO Auto-generated method stub 
     myActivity = arg0[0]; 

     try { 
     getData(); 
     } catch (AuthenticationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } catch (ServiceException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } catch (URISyntaxException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 

     try { 
     String xmlString = XMLCreator.CreateSpreadSheetToXML(); 
     FileOperations.StoreData(xmlString); 
//  Log.d("hello",xmlString); 
     } catch (ParserConfigurationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } catch (TransformerException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     Intent i = new Intent(myActivity.getApplicationContext(),TransApp.class); 
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     myActivity.startActivity(i); 
     myActivity.overridePendingTransition(0, 0); 
     myActivity.finish(); 
    } 

    private void getData() throws AuthenticationException, 
     MalformedURLException, IOException, ServiceException, 
     URISyntaxException { 
     // TODO Auto-generated method stub 
     SpreadsheetService service = new SpreadsheetService(
      "MySpreadsheetIntegration-v1"); 
     service.setProtocolVersion(SpreadsheetService.Versions.V3); 

     URL SPREADSHEET_URL = new URL(
      "https://spreadsheets.google.com/feeds/worksheets/1ZNbR2eECA3PX9RSfFw7R0H50JpALJC50CQLSvvS-ySw/public/full"); 

     WorksheetFeed feed = service.getFeed(SPREADSHEET_URL, 
      WorksheetFeed.class); 
     List<WorksheetEntry> worksheets = feed.getEntries(); 
     WorksheetEntry worksheet = worksheets.get(0); 
     Log.d("hello", "Worksheet name is " 
      + worksheet.getTitle().getPlainText()); 

     // URL listFeedUrl = worksheet.getListFeedUrl(); 
     URL listFeedUrl = new URI(worksheet.getListFeedUrl().toString()) 
      .toURL(); 

     Log.d("hello", "URL is \n " + listFeedUrl.toString()); 

     ListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class); 

     ArrayList<String> CategoryArray = new ArrayList<String>(); 
     ArrayList<String> ShuttleArray = new ArrayList<String>(); 
     ArrayList<String> Email1_Array = new ArrayList<String>(); 
     ArrayList<String> Email2_Array = new ArrayList<String>(); 
     ArrayList<String> RouteArray = new ArrayList<String>(); 
     ArrayList<String> TimeArray = new ArrayList<String>(); 
     ArrayList<String> S_No_Array = new ArrayList<String>(); 
     ArrayList<String> Off1_Array = new ArrayList<String>(); 
     ArrayList<String> Cohort_Array = new ArrayList<String>(); 
     ArrayList<String> NumberStud_Array = new ArrayList<String>(); 
     ArrayList<String> Res2_Array = new ArrayList<String>(); 
     ArrayList<String> Mob1_Array = new ArrayList<String>(); 
     ArrayList<String> Mob2_Array = new ArrayList<String>(); 
     ArrayList<String> Tag_Array = new ArrayList<String>(); 
     ArrayList<String> Day_Array = new ArrayList<String>(); 

     ListEntry TempList = listFeed.getEntries().get(0); 
     for(String tag:TempList.getCustomElements().getTags()){ 
     Tag_Array.add(tag); 
     } 

     for (ListEntry row : listFeed.getEntries()) { 
     for (String tag : row.getCustomElements().getTags()) { 
      if (tag.equals("s.no.")) { 
       S_No_Array.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
      if (tag.equals("route")) { 
       RouteArray.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
      if (tag.equals("time")) { 
       TimeArray.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
      if (tag.equals("category")) { 
       CategoryArray.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
      if (tag.equals("shuttle")) { 
       ShuttleArray.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
      if (tag.equals("o1")) { 
       Off1_Array.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
      if (tag.equals("cohort")) { 
       Cohort_Array.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 

      if (tag.equals("numberofstudents")) { 
       NumberStud_Array.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
      if (tag.equals("r2")) { 
       Res2_Array.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
      if (tag.equals("m1")) { 
       Mob1_Array.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
      if (tag.equals("m2")) { 
       Mob2_Array.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
      if (tag.equals("e1")) { 
       Email1_Array.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
      if (tag.equals("e2")) { 
       Email2_Array.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
      if (tag.equals("day")) { 
       Day_Array.add(ProperValue(row.getCustomElements() 
        .getValue(tag))); 
      } 
     } 

     } 

     XMLParserClass.CategoryArray = CategoryArray; 
     XMLParserClass.ShuttleArray = ShuttleArray; 
     XMLParserClass.Email1_Array = Email1_Array; 
     XMLParserClass.Email2_Array = Email2_Array; 
     XMLParserClass.RouteArray = RouteArray; 
     XMLParserClass.Off1_Array = Off1_Array; 
     XMLParserClass.Cohort_Array = Cohort_Array; 
     XMLParserClass.TimeArray = TimeArray; 
     XMLParserClass.NumberStud_Array = NumberStud_Array; 
     XMLParserClass.Res2_Array = Res2_Array; 
     XMLParserClass.Mob1_Array = Mob1_Array; 
     XMLParserClass.Mob2_Array = Mob2_Array; 
     XMLParserClass.Tag_Array = Tag_Array; 
     XMLParserClass.Day_Array = Day_Array; 

    } 

    public String ProperValue(String input) { 
     if (input == null) { 
     return new String(""); 
     } else { 
     return input; 
     } 

    } 

} 

这完美的作品,但是当我试图编辑电子表格的行,它给了我一个错误: 我用下面的代码:

package com.example.tolotrasamuel.newapispreadsheet; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.util.Log; 

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; 
import com.google.api.client.http.HttpTransport; 
import com.google.api.client.http.javanet.NetHttpTransport; 
import com.google.api.client.json.jackson2.JacksonFactory; 
import com.google.gdata.client.spreadsheet.*; 
import com.google.gdata.data.spreadsheet.*; 
import com.google.gdata.util.*; 

import java.io.File; 
import java.io.IOException; 
import java.net.*; 
import java.security.GeneralSecurityException; 
import java.util.*; 

import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.transform.TransformerException; 

/** 
* Created by Tolotra Samuel on 23/08/2016. 
*/ 
public class SisUpdateMe extends AsyncTask<Activity, Object, Void> { 

    Activity myActivity; 
    @Override 
    protected Void doInBackground(Activity... arg0) { 
     // TODO Auto-generated method stub 
     myActivity = arg0[0]; 

     try { 
      updateaction(); 
     } catch (AuthenticationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ServiceException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (URISyntaxException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 


    public static void updateaction() 
      throws AuthenticationException, MalformedURLException, IOException, ServiceException,URISyntaxException { 

     SpreadsheetService service = 
       new SpreadsheetService("MySpreadsheetIntegration-v1"); 

     service.setProtocolVersion(SpreadsheetService.Versions.V3); 
     File p12 = new File("./key.p12"); 

     HttpTransport httpTransport = new NetHttpTransport(); 
     JacksonFactory jsonFactory = new JacksonFactory(); 
     String[] SCOPESArray = {"https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/1En7qJ5aG2U9fEMSoh-wW1FXoqzNWQfMB4v6-vcN5pVg/private/full", "https://docs.google.com/feeds"}; 
     final List SCOPES = Arrays.asList(SCOPESArray); 
     GoogleCredential credential = null; 
     try { 
      credential = new GoogleCredential.Builder() 
        .setTransport(httpTransport) 
        .setJsonFactory(jsonFactory) 
        .setServiceAccountId("732698423440-ucindhfeddmul5f2dmp42kuv18r3mecq.apps.googleusercontent.com") 
        .setServiceAccountScopes(SCOPES) 
        .setServiceAccountPrivateKeyFromP12File(p12) 
        .build(); 
     } catch (GeneralSecurityException e) { 
      e.printStackTrace(); 
     } 

     service.setOAuth2Credentials(credential); 
     // TODO: Authorize the service object for a specific user (see other sections) 

     // Define the URL to request. This should never change. 
     URL SPREADSHEET_FEED_URL = new URL(
       "https://spreadsheets.google.com/feeds/worksheets/1En7qJ5aG2U9fEMSoh-wW1FXoqzNWQfMB4v6-vcN5pVg/private/full"); 


     WorksheetFeed worksheetFeed = service.getFeed(
       SPREADSHEET_FEED_URL, WorksheetFeed.class); 
     List<WorksheetEntry> worksheets = worksheetFeed.getEntries(); 
     WorksheetEntry worksheet = worksheets.get(0); 
     Log.d("hello", "Worksheet name is " 
       + worksheet.getTitle().getPlainText()); 

     // Fetch the list feed of the worksheet. 
     URL listFeedUrl = new URI(worksheet.getListFeedUrl().toString()) 
       .toURL(); 

     Log.d("hello", "URL is \n " + listFeedUrl.toString()); 

     ListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class); 

     // TODO: Choose a row more intelligently based on your app's needs. 
     ListEntry row = listFeed.getEntries().get(0); 

     // Update the row's data. 
     row.getCustomElements().setValueLocal("route", "Sarah"); 
     row.getCustomElements().setValueLocal("day", "Hunt"); 
     row.getCustomElements().setValueLocal("shuttle", "32"); 
     row.getCustomElements().setValueLocal("category", "154"); 

     // Save the row using the API. 
     row.update(); 
    } 
    } 

我不知道我在哪里做错了。我认为这是验证问题。 如果你能帮忙,我会很高兴。

回答

0

我看到您使用的是Google电子表格API V3。此版本已弃用。相反,请使用新版本:V4。按照此documentation了解如何更新行

相关问题