How to Write/Read in JavaFX with Gluon Plugin - javafx

I am trying to write/read files in JavaFX while using the Gluon plugin, and I am not having any luck. If i run the program as a Desktop App, it works fine, I've even used the same functions in Android IDE and it works fine, however if I send the program to my phone with Gluon, it does not work. I wanting to know how to save/read to files in Gluon, and what have I done wrong, and why does it work as Desktop and on Android but not Gluon. The two fcns I used are:
public void writeToFile() {
try {
FileOutputStream f = new FileOutputStream("Meal1_Protein.txt");
f.write(choice.getBytes()); //Choice is input from a dropdown
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast toast = new Toast(choice);
toast.show();
}
public void readFromFile(){
try {
FileInputStream f = new FileInputStream("Meal1_Protein.txt");
InputStreamReader is = new InputStreamReader(f);
BufferedReader br = new BufferedReader(is);
StringBuffer buff = new StringBuffer();
String lines;
while((lines = br.readLine())!=null){
buff.append(lines);
buff.append("\n");
}
carbInput.setText(buff.toString()); // inserting into a txtfield
//to see if it I can read strings back in.
Toast toast = new Toast(buff.toString());
toast.show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

Related

QR Code Reader for Sony SmartEyeglass

$ Hello, I am using an app I found on Github https://github.com/simsor/SmartEyeglassQRCode/blob/master/AndroidApp/app/src/main/java/com/example/sony/smarteyeglass/extension/helloworld/HelloWorldControl.java, which is a qr code reader for Sony's SmartEyeglass. But for some reason it is not working. When I scan a QRcode, it either says "QR code not found" or "please wait" for a very long time, without ever showing the result. I tried a few things, but nothing changed. Does any of you maybe know what's going on ?
public void processPicture(CameraEvent event) {
updateLayout("Please wait...");
if (event.getIndex() == 0) {
if (event.getData() != null && event.getData().length > 0) {
byte[] data = event.getData();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];
//copy pixel data from the Bitmap into the 'intArray' array
bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);
BinaryBitmap bbmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new QRCodeReader();
int DelayTime = 5000;
boolean error = false;
try {
Result result = reader.decode(bbmap);
Log.d(Constants.LOG_TAG, result.getText());
doWebsiteCommunication(result.getText());
} catch (NotFoundException e) {
updateLayout("QR Code Not Found");
error = true;
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
updateLayout(new String[] {
"QR Code looks corrupted",
"Maybe try again?"
});
error = true;
} catch (FormatException e) {
e.printStackTrace();
updateLayout("That's not a QR Code");
error = true;
}
if (error) {
try {
Thread.sleep(DelayTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentlyTakingPicture = false;
updateLayout(DEFAULT_TEXT);
}
}
}
}
The problem is the
doWebsiteCommunication(result.getText());
Just replace it with
updateLayout(result.getText());
This should work fine
PS: the doWebsiteCommunication was meant to update an external Website with the scandata, but since you just wanna read the qr code you dont need it

getServletContext().getResource(file3) returns null url resource

Here is my code which reads excelsheet files.but i can see my other functions are working fine but when try to get resource as url it return null.
URL resource = null;
try {
System.out.println(file3);
resource = getServletContext().getResource(file3);
System.out.println("Problem with getting resource"+resource);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = null;
try {
System.out.println(resource.toURI());
file = new File(resource.toURI());
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileInputStream input = null;
try {
input = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Workbook w;
w = Workbook.getWorkbook(input);
// Get the first sheet
Sheet sheet = w.getSheet(0);
return sheet;

jgit pull issue HEAD DETACHED

I am trying to use jgit to pull the changes from a git repository.
I am facing minor issues with HEAD DETACHED error when I do a pull
I have read other answers here on stackoverflow. Trying those solutions does not seem to help.
I have 2 remote branches
1. staging
2. master
Here is the sample code:
public static void main(String [] args){
final String GIT_STR = "https://github.com/newlifer2/testNewLiferRepo.git";
final String localRepositoryPath = new File("").getAbsolutePath() + File.separatorChar;
try{
//setting up local repository as just testNewLiferRepo
String repoName = localRepositoryPath + "testNewLiferRepo";
File f = new File(repoName);
Repository localRepo = new FileRepository(repoName + File.separatorChar + ".git");
ObjectId oldObjid = null;
Git git = null;
if (f.exists()) {
oldObjid = localRepo.resolve(Constants.HEAD);
git = Git.open(f);
git.pull().call();
ObjectId currentObjid = localRepo.resolve(Constants.HEAD);
git.getRepository().close();
} else {
git = Git.cloneRepository().setURI(GIT_STR).setDirectory(new File(repoName)).call();
ObjectId currentObjid =localRepo.resolve(Constants.HEAD);
if(!localRepo.getBranch().equalsIgnoreCase("staging")){
git.checkout().setName("refs/remotes/origin/staging").setForce(true).call();
}
git.getRepository().close();
}
} catch (InvalidRemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RevisionSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AmbiguousObjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IncorrectObjectTypeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You can update the version in the testXML file on the repository to test the pull.
Any pointers are highly appreciated
Thanks
Looks like I had my statements switched. Here is a new solution
public static void main(String [] args){
final String GIT_STR = "https://github.com/newlifer2/testNewLiferRepo.git";
final String localRepositoryPath = new File("").getAbsolutePath() + File.separatorChar;
SSHSessionFactory sessionFactory = new SSHSessionFactory();
sessionFactory.Initialize();
SshSessionFactory.setInstance(sessionFactory);
try{
//setting up local repository as just testNewLiferRepo
String repoName = localRepositoryPath + "testNewLiferRepo";
File f = new File(repoName);
Repository localRepo = new FileRepository(repoName + File.separatorChar + ".git");
ObjectId oldObjid = null;
Git git = null;
if (f.exists()) {
oldObjid = localRepo.resolve(Constants.HEAD);
git = Git.open(f);
Map<String,Ref> m = git.getRepository().getAllRefs();
System.out.println("Current Branch: "+git.getRepository().getBranch());
if(git.getRepository().isValidRefName("refs/remotes/origin/staging")){
System.out.println("Valid");
}
git.pull().call();
git.checkout().setName("staging").call();
ObjectId currentObjid = localRepo.resolve(Constants.HEAD);
git.getRepository().close();
} else {
git = Git.cloneRepository().setURI(GIT_STR).setDirectory(new File(repoName)).call();
ObjectId currentObjid =localRepo.resolve(Constants.HEAD);
if(!localRepo.getBranch().equalsIgnoreCase("staging")){
git.branchCreate().setForce(true).setName("staging").setStartPoint("refs/remotes/origin/staging").call();
}
git.checkout().setName("staging").call();
git.getRepository().close();
}
} catch (InvalidRemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RevisionSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AmbiguousObjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IncorrectObjectTypeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Reading InputStream of NANOHTTPD gives Socket TimeOut Exception

I am trying to read the InputStream from IHTTPSession.getInputStream() using the following code but its gives Socket TimeOut Exception every time.
private String readInStream(InputStream in){
StringBuffer outBuffer=new StringBuffer();
BufferedInputStream bis=new BufferedInputStream(in);
try {
while(bis.available()>0){
int ch= bis.read();
outBuffer.append((char)ch);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("DATA_Length", "outputBuffer :"+outBuffer.toString().length());
return outBuffer.toString();
}
i also tried the following Method but the same exception arises
private String readInStream(InputStream in){
String line="";
StringBuffer outBuffer=new StringBuffer();
BufferedReader rd=new BufferedReader(new InputStreamReader(in));
try {
while((line=rd.readLine()) != null){
outBuffer.append(line);
}
} catch (IOException e) {
Log.e("IOException", "IOException in readInStream:");
e.printStackTrace();
}
Log.e("DATA_Length", "outputBuffer :"+outBuffer.toString().length());
return outBuffer.toString();
}
Getting the content length from the header and reading up to it solved the problem.
Can confirm the accepted answer works (getting content length from the header and reading up to it). Here is some example code to turn the InputStream into a String:
try {
int contentLength = Integer.valueOf(session.getHeaders().get("content-length"));
String msg = new String(in.readNBytes(contentLength)); // the request body
} catch (IOException e) {
e.printStackTrace();
}
If you want to prevent a NullPointerException here, check whether the content-length header actually exists before parsing it to an integer.

WebClient DownloadStringCompleted Never Fired in Console Application

I am not sure why the callback methods are not fired AT ALL. I am using VS 2010.
static void Main(string[] args)
{
try
{
var url = "some link to RSS FEED";
var client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
client.DownloadStringAsync(new Uri(url));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
// THIS IS NEVER FIRED
static void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
Console.WriteLine("something");
}
// THIS IS NEVER FIRED
static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
Console.WriteLine("do something");
var rss = XElement.Parse(e.Result);
var pictures = from item in rss.Descendants("channel")
select new Picture
{
Name = item.Element("title").Value
};
foreach (var picture in pictures)
{
Console.WriteLine(picture.Name);
Console.WriteLine(picture.Url);
}
}
The DownloadDataCompleted event is fired if you call the DownloadDataAsync() method. DownloadStringCompleted is fired if you call the DownloadStringAsync() method.
To get the DownloadDataCompleted event to fire, try:
static void Main(string[] args)
{
try
{
var url = "http://blog.gravitypad.com";
//client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
client.DownloadDataAsync(new Uri(url));
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
I had this problem and realized that the uri was not correct. I mean the event wont fire unless the file is read correctly. So I placed my xml file in ClientBin and it worked like magic!

Resources