How to access FTP from Android Application?

I ran into trouble while writing a piece of code to transfer a bunch of images to my server from a custom app. People recommended Apache FTP for Java. But it can't be used as is since Honeycomb. It needs the task to run on a different thread. This makes the UI far more responsive and is a forced better way to write programs for Android.

Step 1:
First download the zip file called Apache Commons Net from
http://commons.apache.org/net/download_net.cgi

Step 2:
Extract the zip file and copy the file called commons-net-xx.jar Into the libs folder of your Android eclipse project



Step 3: Create a new Java Class with the following code.


package com.example.technologyempoweredjanatainitiative;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import android.os.AsyncTask;
import android.util.Log;


public class FTP_Uploader_AsyncTask extends AsyncTask {

//void FTP_DATA_UPLOAD(String FULL_PATH_TO_LOCAL_FILE)


protected Long doInBackground(String... FULL_PATH_TO_LOCAL_FILE ) {
// encapsulate FTP inside a A sync task

{
System.out.println("Entered FTP transfer function");

FTPClient ftpClient = new FTPClient();
int reply;
try {
System.out.println("Entered Data Upload loop!");
   ftpClient.connect("Ftp.example.com",21);
   ftpClient.login("username", "password");
   ftpClient.changeWorkingDirectory("/directory/");
   System.out.println("Entered Data Upload loop!");
 

   reply = ftpClient.getReplyCode();

 
  if(FTPReply.isPositiveCompletion(reply)){
  System.out.println("Connected Success");
  }else {
  System.out.println("Connection Failed");
  ftpClient.disconnect();
  }
 
 
 
   if (ftpClient.getReplyString().contains("250")) {
       ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
       BufferedInputStream buffIn = null;
       System.out.println("Created an input stream buffer");
       System.out.println(FULL_PATH_TO_LOCAL_FILE.toString());
     
       buffIn = new BufferedInputStream(new FileInputStream(FULL_PATH_TO_LOCAL_FILE[0]));
       ftpClient.enterLocalPassiveMode();
     
       System.out.println("Entered binary and passive modes");
       //Handler progressHandler=null;
//ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler);

       //Code to Extract name from the string.
       //http://stackoverflow.com/questions/10549504/obtain-name-from-absolute-path-substring-from-last-slash-java-android
       String Picture_File_name = new File(FULL_PATH_TO_LOCAL_FILE[0]).getName();
     
     
       boolean result = ftpClient.storeFile(Picture_File_name, buffIn); //localAsset.getFileName()
       //ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler);
     
       if (result){
        System.out.println("Success");
       }
     
       //boolean result = ftpClient.storeFile("TEST.jpg", progressInput);
       System.out.println("File saved");
     
     
       //buffIn.close();
       ftpClient.logout();
       ftpClient.disconnect();
   }

} catch (SocketException e) {
   Log.e("Citizen Cam FTP", e.getStackTrace().toString());
   System.out.println("Socket Exception!");
} catch (UnknownHostException e) {
   Log.e("Citizen Cam FTP", e.getStackTrace().toString());
} catch (IOException e) {
   Log.e("Citizen Cam FTP", e.getStackTrace().toString());
   System.out.println("IO Exception!");
}

return null;
}


}

}


To access the ftp  service to upload files call the above class using : 

new FTP_Uploader_AsyncTask().execute(PathForFileToUpload);

How to use Python for coding with ZPL?

ZPL-Zebra Programming Language is used for communicating with Zebra Printers. They are mostly used for RFID and Bar-code printing. As ever, I wanted a bit more control over how my ZPL printer worked and had to get working.
The easiest technique is of course Python. And this is how I did it.

First get the Zebra package from Pypi
pypi.python.org/pypi/zebra/#downloads

Installation is simple. From commandline navigate to the folder containing setup.py and then enter
python setup.py install   [Windows Only]

I presume you've installed all necessary drivers. If you are using Win 7, Windows would have automatically installed them for you. If you're on Linux, you'd need a CUPS driver. Just look at the documentation.

Example Program:
###START OF CODE###

#ZPL commands to be sent to your Printer
label="""
^XA
^FO10,10
^A0,40,40
^FD
Hello World
^FS
^XZ
"""

#End of ZPL commands to print Hello World

from zebra import zebra
z=zebra
z.getqueues                                      #This will return a list of printers installed on your computer. 
z.setqueues('ZDesigner_ZPL_200')  #Set the default printer to your new ZPL printer
z.output(label)                                  #Have fun sending data to your printer.It's as easy as it can get. :)


####END OF CODE####


Reference:
http://bytes.com/topic/python/answers/24160-zebra-printing-language
http://pypi.python.org/pypi/zebra/

How to access Parallel Ports on Windows XP?

I needed to get access to the Parallel/Seial Ports on my new XP. But it seemed crazy with almost nothing working.
Here's a simple way to access the same.

First get Userport
http://www.embeddedtronics.com:80/public/Electronics/minidaq/userport/UserPort.zip 

http://booksbybibin.14.forumer.com/viewtopic.php?t=74

http://neil.fraser.name/software/lpt/

Now get Active Python 2.6.7 and import Inpout 32.dll
logix4u.net/parallel-port/16-inpout32dll-for-windows-982000ntxp

http://blog.wensheng.com/2008/10/python-parallel-port-on-windows.html

from ctypes import windll
p = windll.inpout32
p.Inp32(0x378) #default 255(all high) on my pc 
p.Out32(0x378, 0) #put all low on port 2-9

his tutorial is taken from the book 
MY EXPERIENCE IN PARALLEL PORT INTERFACING. 
you can download it free from the files
section(file:-parallelport(3 files) of
yahoo group-booksbybibin
 
http://groups.yahoo.com/group/booksbybibin/ 

comments about this book on 
www.booksbybibin.blogspot.com 

SOFTWARES USED IN WINDOWS 
All operating systems of Windows upto Window 
ME(included) requires no driver software for parallel port. You can interface it by a 
simple program in Turbo C. But operating systems after Windows ME have a security 
to parallel port. So we have to break it inorder to interface with outside circuits. This 
is done by using USERPORT. 
Installing USERPORT: 
 

You can get userport.zip with this file or from yahoo group-parallelport. 
http://www.embeddedtronics.com:80/public/Electronics/minidaq/userport/UserPort.zip 
Download this file and unzip it 
then copy Userport.sys to WINDOWSSYSTEM32DRIVERS 
then run USERPORT.EXE 
u will get a message driver started. Click Start two times then you will get the 
following message. This is the next step after BIOS Settings. 
Now u do programming in C/C++/JAVA and interface in XP or other version of 
Windows. USERPORT is necessary software you should install. If you are using VB or 
VC then you can get DLL's from 
www.logix4u.net 

USING LPT.EXE: 
This is a small VB program which shows the status of the parallel port registers 
You can get it with this book or yahoo group-parallelport or from the link 
http://neil.fraser.name/software/lpt/ 
After BIOS settings and userport installation next step is to check whether your 
parallel port will work or not and which ports will work well. 
Run lpt.exe 
 

select your port address first as you set on the BIOS. The following diagram shows 
how to check port. You 'tick' indicates high on that port. So change tick on the port. 
Make it low and high. If it is changing for data register and control register then your 
parallel port is working fine and will work fine. But if the 'tick' is not changing then 
you won't be able to use it. You won't be able to use the ports in which 'ticks' are not 
changing. In the above diagram we can see that Pin1 (strobe C0) is not having tick(i.e 
is low), but if you connect an LED then it will glow because C0' is there and the port get's HIGH. Suppose if you input LOW on pin1 then the Strobe will get HIGH in the software. So remember this when you do troubleshooting circuits with lpt. 

Category: 0 comments

Contributors