Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, May 4, 2013

[Android Tips]: Uploading Images to a Remote Web Service

upload

In order to upload images to a web service that requires a Multipart request, the below code will create the file part object and include it as part of the MultiPartRequest entity. I store this method in a separate class and call it statically from within an AsyncTask.  This type of functionality is particularly useful for when you want to upload a user's profile photo and store it on your remote file system.

The following Apache libraries are required:

  • commons-codec.jar (version used: 1.3)

  • commons-httpclient.jar (version used: 3.0-rc4)

  • commons-logging.jar


[code language="java"]
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;

static boolean doImageUpload(final byte[] imageData, String fileName) throws Exception {
String responseString = null;

PostMethod method = new PostMethod(<remote url>);
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(100000);

FilePart photo = new FilePart("file-data", new ByteArrayPartSource(fileName, imageData));
photo.setContentType("image/jpeg");
photo.setCharSet(null);

Part[] parts = {photo};
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
client.executeMethod(method);

responseString = method.getResponseBodyAsString();
method.releaseConnection();

if (responseString.equals(<successful response>)) {
return true;
} else {
return false;
}
}
[/code]

Have you done this a different way?  Leave a comment below.

*** Download our first game: Word Crank today for free: Available on iOS and Android***

Monday, August 29, 2011

Tips: Programming for Android and iOS

As I continue my foray into game development on the iOS and Android platforms I wanted to take a few moments to share some tips that I have found to be very useful in creating an application that can will eventually run on both platforms.

Tips


General



  1. Separate the game logic from the user interface layer.  This will make it easier to port the code over to the accompanying platform.  For example, if you put the main game loop logic in a separate Java class, then you basically can just do a line by line conversion to Objective-C when you port it to iOS.

  2. Place all Strings in one location.  Have a Constants.java where you put all of your static String objects, then when you port to iOS you just use the same thing, except now it is Constants.h.

  3. If you are going to use Texture Atlases in your game, create them using a tool such as Texture Packer and then you will have the exact location of each Sprite stored in a file that can be easily referenced for both versions of the game.


iOS


Use a framework! Working with Cocos2d made the transition relatively smooth as we converted Word Crank from its Android version to iOS.

Android


Get a good book, I read Beginning Android Games, it helped lay a good foundation for working with Android and understanding the various states of a running application and the GLSurfaceView that serves as the main background for all OpenGL drawings.

Any questions? Just leave a comment. Thanks.

Thursday, August 25, 2011

Application Crash Reporting -- Android Style

In order to provide users with an error-free as possible application, it's imperative that you use some form of Crash Reporting. The ACRA library which is available on Android provides such a framework. Here is how they describe their tool:
"ACRA is a library enabling Android Application to automatically post their crash reports to a GoogleDoc  form. It is targeted to android applications developers to help them get data from their applications when they crash or behave erroneously."

We have integrated the ACRA library into our application, Word Crank, so far I have been able to fix 3 major bugs that did not turn up in testing, but were clearly called out in the Google Doc created by the ACRA tool. We chose to go with the most basic functionality that of sending a Crash Report every time the application Force Closes. However, it is very configurable and can display status messages, custom Android Toasts, etc.

I highly recommend this tool for anyone developing an Android Application. You can download it here: ACRA Crash Reporting Tool

Thursday, June 30, 2011

Converting an Integer to an Int Array

During game development one thing that you want to avoid as much as possible is allocating memory in your main game loop.  One item that typically changes often during game play is the user's score.  In order to avoid creating new objects to store the user's updated score value, you can store each digit of the new score in an array.  Then use a simple method to display the portion of your texture, or image graphic, that maps to each digit.  The method that I used to convert from an Integer to an int array is shown below.

public static void setScore(int[] score, int points) {
int temp = points;
int i = 0;
while (temp > 0) {
score[i] = temp % 10;
temp /= 10;
i++;
}
}