2016-08-23 24 views
0

アンドロイドスタジオを使用してアプリのコードを作成しています。これまでは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に従ってください

関連する問題