Hi I'm trying to insert a row in a google worksheet, but I can not do it becouse have this error:
Exception in thread "main" com.google.gdata.util.ServiceException: Method Not Allowed
at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:632)
at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)
at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
at com.google.gdata.client.Service.insert(Service.java:1409)
at com.google.gdata.client.GoogleService.insert(GoogleService.java:613)
at it.unical.mat.spreadsheet.Main.main(Main.java:64)
Please help my to resolve this problem, this is my code:
public class MyClass {
public static void main(String[] args)
throws AuthenticationException, MalformedURLException, IOException, ServiceException
{
SpreadsheetService service = new SpreadsheetService("MyApp");
FeedURLFactory factory = FeedURLFactory.getDefault();
String key = "***my_key***";
URL spreadSheetUrl = factory.getWorksheetFeedUrl(key,"public","full");
WorksheetFeed feed = service.getFeed(spreadSheetUrl, WorksheetFeed.class);
WorksheetEntry worksheet = feed.getEntries().get(13);
URL url = worksheet.getListFeedUrl();
ListEntry row = new ListEntry();
row.getCustomElements().setValueLocal("header", "aaa");
row = service.insert(url, row);
}
}
My intent is to insert a new row in a google worksheet!
If reading rows I don't have problems, but the problem is when write in a rows!
Related
I am having trouble sending my test results from selenium to testrail. I cant seem to figure it out using the paperwork provided. I am currently using this:
public class login_errors extends ConditionsWebDriverFactory {
public static String TEST_RUN_ID = "R1713";
public static String TESTRAIL_USERNAME = "testemai9#hotmail.com";
public static String TESTRAIL_PASSWORD = "Password1";
public static String RAILS_ENGINE_URL = "https://testproj.testrail.com/";
public static final int TEST_CASE_PASSED_STATUS = 1;
public static final int TEST_CASE_FAILED_STATUS = 5;
#Test
public void login_errors() throws IOException, APIException {
Header header = new Header();
header.guest_select_login();
Pages.Login login = new Pages.Login();
login.login_with_empty_fields();
login.login_with_invalid_email();
login.email_or_password_incorrect();
login.login_open_and_close();
login_errors.addResultForTestCase(TEST_RUN_ID,TEST_CASE_PASSED_STATUS," ");
}
public static void addResultForTestCase(String testCaseId, int status,
String error) throws IOException, APIException {
String testRunId = TEST_RUN_ID;
APIClient client = new APIClient(RAILS_ENGINE_URL);
client.setUser(TESTRAIL_USERNAME);
client.setPassword(TESTRAIL_PASSWORD);
Map data = new HashMap();
data.put("status_id", status);
data.put("comment", "Test Executed - Status updated automatically from Selenium test automation.");
client.sendPost("add_result_for_case/"+testRunId+"/"+testCaseId+"",data );
}
}
But i keep getting the following exception:
com.gurock.testrail.APIException: TestRail API returned HTTP
401("Authentication failed: invalid or missing user/password or
session cookie.")
Can anybody help me on the exact way this should be done out in java? I am not sure I am doing it correctly.
I'm using my own custom made API for testrail, but it is all based on same thing.
But looking to official gurrock,
documentation
First You need to add "testrail/" at the end of Your URL endpoint,
APIClient client = new APIClient("http://<server>/testrail/");
client.setUser("..");
client.setPassword("..");
Should be like this:
public static String RAILS_ENGINE_URL ="https://testproj.testrail.com/testrail/";
Second thing what I found out is that You're sending test run id in variable for testcase ID, this is not same.
And another thing is that test case ID shouldn't be with anything in front just pure number, not like "R123" but "123"
And Your method should than accept one more parameter, testRunId
public static void addResultForTestCase(String testRunId, String testCaseId, int status,String error) throws IOException, APIException {
String testRunId = TEST_RUN_ID;
APIClient client = new APIClient(RAILS_ENGINE_URL);
client.setUser(TESTRAIL_USERNAME);
client.setPassword(TESTRAIL_PASSWORD);
Map data = new HashMap();
data.put("status_id", status);
data.put("comment", "Test Executed - Status updated automatically from Selenium test automation.");
client.sendPost("add_result_for_case/"+testRunId+"/"+testCaseId+"",data );
}
And another thing is that You have to have created testrun, so You can have Yourself your testrun ID, like pic bellow:
And than test case ID is different from testrun id.
Uses on-line decomentation I come up with the following code to terminate the current EC2 Instance:
public class Ec2Utility {
static private final String LOCAL_META_DATA_ENDPOINT = "http://169.254.169.254/latest/meta-data/";
static private final String LOCAL_INSTANCE_ID_SERVICE = "instance-id";
static public void terminateMe() throws Exception {
TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest().withInstanceIds(getInstanceId());
AmazonEC2 ec2 = new AmazonEC2Client();
ec2.terminateInstances(terminateRequest);
}
static public String getInstanceId() throws Exception {
//SimpleRestClient, is an internal wrapper on http client.
SimpleRestClient client = new SimpleRestClient(LOCAL_META_DATA_ENDPOINT);
HttpResponse response = client.makeRequest(METHOD.GET, LOCAL_INSTANCE_ID_SERVICE);
return IOUtils.toString(response.getEntity().getContent(), "UTF-8");
}
}
My issue is that my EC2 instance is under an AutoScalingGroup which is under a CloudFormationStack, that is because of my organisation deployment standards though this single EC2 is all there is there for this feature.
So, I want to terminate the entire CloudFormationStack from the JavaSDK, keep in mind, I don't have the CloudFormation Stack Name in advance as I didn't have the EC2 Instance Id so I will have to get it from the code using the API calls.
How can I do that, if I can do it?
you should be able to use the deleteStack method from cloud formation sdk
DeleteStackRequest request = new DeleteStackRequest();
request.setStackName(<stack_name_to_be_deleted>);
AmazonCloudFormationClient client = new AmazonCloudFormationClient (<credentials>);
client.deleteStack(request);
If you don't have the stack name, you should be able to retrieve from the Tag of your instance
DescribeInstancesRequest request =new DescribeInstancesRequest();
request.setInstanceIds(instancesList);
DescribeInstancesResult disresult = ec2.describeInstances(request);
List <Reservation> list = disresult.getReservations();
for (Reservation res:list){
List <Instance> instancelist = res.getInstances();
for (Instance instance:instancelist){
List <Tag> tags = instance.getTags();
for (Tag tag:tags){
if (tag.getKey().equals("aws:cloudformation:stack-name")) {
tag.getValue(); // name of the stack
}
}
At the end I've achieved the desired behaviour using the set of the following util functions I wrote:
/**
* Delete the CloudFormationStack with the given name.
*
* #param stackName
* #throws Exception
*/
static public void deleteCloudFormationStack(String stackName) throws Exception {
AmazonCloudFormationClient client = new AmazonCloudFormationClient();
DeleteStackRequest deleteStackRequest = new DeleteStackRequest().withStackName("");
client.deleteStack(deleteStackRequest);
}
static public String getCloudFormationStackName() throws Exception {
AmazonEC2 ec2 = new AmazonEC2Client();
String instanceId = getInstanceId();
List<Tag> tags = getEc2Tags(ec2, instanceId);
for (Tag t : tags) {
if (t.getKey().equalsIgnoreCase(TAG_KEY_STACK_NAME)) {
return t.getValue();
}
}
throw new Exception("Couldn't find stack name for instanceId:" + instanceId);
}
static private List<Tag> getEc2Tags(AmazonEC2 ec2, String instanceId) throws Exception {
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(instanceId);
DescribeInstancesResult describeInstances = ec2.describeInstances(describeInstancesRequest);
List<Reservation> reservations = describeInstances.getReservations();
if (reservations.isEmpty()) {
throw new Exception("DescribeInstances didn't returned reservation for instanceId:" + instanceId);
}
List<Instance> instances = reservations.get(0).getInstances();
if (instances.isEmpty()) {
throw new Exception("DescribeInstances didn't returned instance for instanceId:" + instanceId);
}
return instances.get(0).getTags();
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// Example of usage from the code:
deleteCloudFormationStack(getCloudFormationStackName());
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
How can I insert a row in a Spreadsheet?
I try with this code but have an error:
Exception in thread "main" com.google.gdata.util.ServiceException: Method Not Allowed
at line: row = service.insert(url, row);
Why Method Not Allowed ???
public class MyClass {
public static void main(String[] args)
throws AuthenticationException, MalformedURLException, IOException, ServiceException
{
SpreadsheetService service = new SpreadsheetService("MyApp");
FeedURLFactory factory = FeedURLFactory.getDefault();
String key = "***my_key***";
URL spreadSheetUrl = factory.getWorksheetFeedUrl(key,"public","full");
WorksheetFeed feed = service.getFeed(spreadSheetUrl, WorksheetFeed.class);
WorksheetEntry worksheet = feed.getEntries().get(13);
URL url = worksheet.getListFeedUrl();
ListEntry row = new ListEntry();
row.getCustomElements().setValueLocal("header", "aaa");
row = service.insert(url, row);
}
}
Help me! thanks
A similar problem has been found here. Perhaps you are not using the full correct formal for the URL, but I can understand why you wouldn't want to post that publicly. However, it's hard to tell if that it the problem or not without seeing it.
I am trying to use google spreadsheet as a simple database in android. But when I make calls to the spread sheet, I get NullpointerExeptions
when I tried to tried to get the size of the worksheet, it returned 0.
for example, when I call getAllCells() I get a NullpointerExeption.
I appreciate any help/ suggestions you can offer.
(I made the spreadsheet published to the web and Anyone with the link can edit )
here is the spreadsheet https://docs.google.com/spreadsheets/d/1arJFUxSghwdv0QpnJJ3lBu36X4I3d_uB4xx_xNGLKHU/edit#gid=0
here is the zip file of the small project https://drive.google.com/file/d/0B36AnWs56yGIdVFpdzNxSE9BSlU/view?usp=sharing
here is the apk
https://drive.google.com/file/d/0B36AnWs56yGITkIyUGpJcHpuUlk/view?usp=sharing
here is the error
java.lang.NullPointerException
1at com.google.gdata.data.spreadsheet.WorksheetEntry.getFeedUrlString(WorksheetEntry.java:133)
2at com.google.gdata.data.spreadsheet.WorksheetEntry.getCellFeedUrl(WorksheetEntry.java:113)
3at com.example.tempo.util.SpDatabase.getAllCells(SpDatabase.java:67)
4at com.example.tempo.TempoMain.testDataBase(TempoMain.java:101)
5at com.example.tempo.TempoMain$1$1.doOnBackground(TempoMain.java:70)
6at com.example.tempo.util.AsyncJob$2.run(AsyncJob.java:59)
7at java.lang.Thread.run(Thread.java:841)
Here is the java class I use to access it
package com.example.tempo.util;
import android.widget.Toast;
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.*;
import java.util.*;
public class SpDatabase {
WorksheetEntry worksheet;
SpreadsheetService service;
String spTitle;
public SpDatabase()throws AuthenticationException, MalformedURLException, IOException, ServiceException {
service = new SpreadsheetService("MySpreadsheetIntegration-v1");
// TODO: Authorize the service object for a specific user (see other sections)
// Define the URL to request. This should never change.
String key = "1arJFUxSghwdv0QpnJJ3lBu36X4I3d_uB4xx_xNGLKHU";
URL SPREADSHEET_FEED_URL = FeedURLFactory.getDefault().getWorksheetFeedUrl(key, "public", "basic");
// Make a request to the API and get all spreadsheets.
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
if (spreadsheets.size() == 0) {
// TODO: There were no spreadsheets, act accordingly.
}
// TODO: Choose a spreadsheet more intelligently based on your
// app's needs.
SpreadsheetEntry spreadsheet = spreadsheets.get(0);
spTitle = spreadsheet.getTitle().getPlainText();
// Make a request to the API to fetch information about all
// worksheets in the spreadsheet.
List<WorksheetEntry> worksheets = spreadsheet.getWorksheets();
// WorksheetEntry worksheet = worksheets.get(0);
worksheet = worksheets.get(0);
}
public int getRowCount(){
int rowCount = worksheet.getRowCount();
return rowCount;
}
public int getColumnCount(){
int columnCount = worksheet.getColCount();
return columnCount;
}
public ArrayList<CellEntry> getAllCells() throws IOException, ServiceException{
// Fetch the cell feed of the worksheet.
URL cellFeedUrl = worksheet.getCellFeedUrl();
CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);
return (ArrayList<CellEntry>) cellFeed.getEntries();
}
// or getRow() or getCollumn()
public ArrayList<CellEntry> getSpecificCells(int minRow, int maxRow, int minCol, int maxCol) throws IOException, ServiceException, URISyntaxException{
//example: Fetch column 4, and every row after row 1 --> "?min-row=2&min-col=4&max-col=4"
String pref = "?";
if(minRow != -1){
pref = appendAndIfFirstValue(pref);
pref += "min-row=" + minRow;
}
if(minCol != -1){
pref = appendAndIfFirstValue(pref);
pref += "&min-col=" + minCol;
}
if(maxRow != -1){
pref = appendAndIfFirstValue(pref);
pref += "&max-row=" + maxRow;
}
if(maxCol != -1){
pref = appendAndIfFirstValue(pref);
pref += "&max-col=" + maxCol;
}
// Fetch column 4, and every row after row 1.
URL cellFeedUrl = new URI(worksheet.getCellFeedUrl().toString() + pref).toURL();
CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);
return (ArrayList<CellEntry>) cellFeed.getEntries();
}
public int getCellNumValue(CellEntry cell){
return (Integer) cell.getCell().getNumericValue();
}
public String getCellStringValue(CellEntry cell){
return cell.getCell().getValue();
}
public void incrementCellValue(CellEntry cell) throws IOException, ServiceException{
String cellID = cell.getTitle().getPlainText();
cell.changeInputValueLocal("=SUM(" + cellID + ", 1)");
cell.update();
}
public void changeCellValue(CellEntry cell, String value) throws IOException, ServiceException{
cell.changeInputValueLocal(value);
cell.update();
}
private String appendAndIfFirstValue(String str){
if(!str.contains("&")){
return "&" + str;
}else{
return str;
}
}
public String getSpreadSheetTitle() throws IOException, ServiceException, URISyntaxException{
return spTitle;
}
}
It turns out the problem was the scope of the projection and arcording to the the documentation google said The spreadsheets feed only supports the 'private' visibility and the 'full' projection.
I was using the 'public' and 'basic'
so what I did to fix the problem was to access the worksheets(which supports more visibility parameters) directly like this:
public SpDatabase()throws AuthenticationException, MalformedURLException, IOException, ServiceException {
service = new SpreadsheetService("Test");
FeedURLFactory factory = FeedURLFactory.getDefault();
String key = "1arJFUxSghwdv0QpnJJ3lBu36X4I3d_uB4xx_xNGLKHU";
URL spreadSheetUrl = factory.getWorksheetFeedUrl(key, "public", "full");
WorksheetFeed feed = service.getFeed(spreadSheetUrl, WorksheetFeed.class);
worksheet = feed.getEntries().get(0);
URL cellFeedURL = worksheet.getCellFeedUrl();
CellFeed cellFeed = service.getFeed(cellFeedURL, CellFeed.class);
}
I'm trying to read data attribute from its tag in Java with the library pixelmed.
the code that I had is :
public static void main(String args[]) throws IOException, DicomException {
DicomInputStream my_image = new DicomInputStream(new File("/Volumes/CDP/20130212101717421/20130212101636203"));
AttributeList list = new AttributeList();
SpecificCharacterSet sc=new SpecificCharacterSet(list);
PersonNameAttribute pna=new PersonNameAttribute(TagFromName.PatientName,1000,my_image,sc);
System.out.println(pna.getDelimitedStringValuesOrEmptyString());
}
With this code i get data of all attributes :
���UL������OB��������UI�1.2.840.10008.5.1.4.1.1.4���UI6�1.2.840.113619.2.244.6945.224850.21460.1360606914.740���UI�1.2.840.10008.1.2.1���UI�1.2.376.99999.1.1.20041017��SH�CDP_V3��AE�MRS��CS
�ISO_IR ... etc etc
But I just want to get the information on the tag (0x0010,0x0010).
Have you considered:
AttributeList list = new AttributeList();
list.read(file);
String patientName=Attribute.getDelimitedStringValuesOrEmptyString(list,TagFromName.PatientName);