Hi all,
I had a problem that how to communicate with the xmpp server in Android. I firstly used the most powerful library smack for this purpose but with the passage of time I had come to know that it is not easy and not much flexible and it is good for the desktop or web applications. So, I started a research on the internet and at last I found the JXA library which is very light weight and more flexible than any other library.
Use this library and enjoy.
Tuesday, October 12, 2010
Tuesday, October 5, 2010
Android Screen Transitions
We can change the default transition in android but it is not necessary that our given transition apply every time. We should create some animation xml files and follow the steps below:
Steps to create activity transitions.
· Create a folder in the res folder.
· Named the new created folder as “anim”
· Put XMLs of animation in it.
· Call the following method of
o overridePendingTransition(R.anim.xml_file, R.anim.xml_file);
o first parameter is the start activity transitions and second is current activity.
o it should be call immediately after one of the flavors of startActivity(Intent) or finish() to specify an explicit transition animation to perform next.
· There are number of animations.
o Alpha
o Scale
o Rotate
Wednesday, September 22, 2010
Android File Upload
This is the code for upload the file from the android phone to php server.
public void upload(String fileName, String filePath, String strURL) {
if (fileName == null || fileName.equals("")) {
throw new RuntimeException("FileName cannot be empty");
}
if (filePath == null || filePath.equals("")) {
throw new RuntimeException("FilePath cannot be empty");
}
if (strURL == null || strURL.equals("") && !TextValidation.isValidURL(strURL)) {
throw new RuntimeException("Url cannot be empty");
}
// Get the file input stream
FileInputStream fis = setFile(filePath);
// indicator for the upload file white spaces
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String exsistingFileName = fileName;
try {
// Http connection and parameter setting
URL connectURL = new URL(strURL);
HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// Data Stream for sending file
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"userfile\";filename=\"" + exsistingFileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
int bytesAvailable = fis.available();
int maxBufferSize = 1024 * 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead = fis.read(buffer, 0, bufferSize);
// output stream populate
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fis.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fis.read(buffer, 0, bufferSize);
}
// actual upload
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close the input stream
fis.close();
// flush the data output stream
dos.flush();
// response received
InputStream is = conn.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
Log.i("Response", ">" + b.toString());
} catch (Exception ex) {
Log.e("Exception", ">" + ex.toString());
throw new RuntimeException("Exception: " + ex.toString());
}
}
public void upload(String fileName, String filePath, String strURL) {
if (fileName == null || fileName.equals("")) {
throw new RuntimeException("FileName cannot be empty");
}
if (filePath == null || filePath.equals("")) {
throw new RuntimeException("FilePath cannot be empty");
}
if (strURL == null || strURL.equals("") && !TextValidation.isValidURL(strURL)) {
throw new RuntimeException("Url cannot be empty");
}
// Get the file input stream
FileInputStream fis = setFile(filePath);
// indicator for the upload file white spaces
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String exsistingFileName = fileName;
try {
// Http connection and parameter setting
URL connectURL = new URL(strURL);
HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// Data Stream for sending file
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"userfile\";filename=\"" + exsistingFileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
int bytesAvailable = fis.available();
int maxBufferSize = 1024 * 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead = fis.read(buffer, 0, bufferSize);
// output stream populate
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fis.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fis.read(buffer, 0, bufferSize);
}
// actual upload
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close the input stream
fis.close();
// flush the data output stream
dos.flush();
// response received
InputStream is = conn.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
Log.i("Response", ">" + b.toString());
} catch (Exception ex) {
Log.e("Exception", ">" + ex.toString());
throw new RuntimeException("Exception: " + ex.toString());
}
}
Subscribe to:
Posts (Atom)