Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Java / Android + Retrofit: IllegalArgumentException: Only one HTTP method is allowed. Found: POST and POST.

What a terribly strange and cryptic error message to come across and totally undescriptive, so of course you've Googled it.

The source of the problem is actually quite easy to fix.

Take a look at your RetrofitAPI class. It'll probably look something like this:

@POST("/rest/events/event/{id}/")
Item saveEvent(@Path("id") int id, @Field("type") String type, @Field("title") String title, @Field("details") String details);

Well you're missing one more thing, the @FormUrlEncoded decorator!

That should fix up your problems.

Wl4AJzx

Java/Android: Access private variables in a class using reflection

Android's DatePickerDialog class has a handy little function getDatePicker(), which exposes the private DatePicker mDatePicker.

Problem with that is it's only available on Honeycomb/API 11+. I don't want to raise my damn API level and leave 2.3 users behind! They're the majority of my users (at time of writing)

funny-gifs-table-flip

So, what can I do to get around this design oversight? Extending/subclassing the DatePickerDialog class won't work, because it's still a private variable.

Good thing is that even on Android, reflection is still a valid option. What reflection does is use Java's internal mechanisms to access the variable directly from the object.

public DatePicker getDatePicker() {
try {
Field field = DatePickerDialog.class.getDeclaredField("mDatePicker");
field.setAccessible(true);
return (DatePicker) field.get(datePickerDialog);
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}

return null;
}

This little helper function works a treat! Unless the variable is renamed in other revisions of Android, it should work fine in all cases.

Sources

Java: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

If you've ever encountered this error, it means your project (or the application you're trying to run) has used too much memory.

To remedy this, find the script that is executing it and add in a new argument "-Xmx1024m".

This will set the maximum heap size to 1024mb. You can use any other value, but I was parsing a big Android method trace file using traceview.

For example, "traceview.bat" contains this line at the end:

call %java_exe% -Djava.ext.dirs=%javaextdirs% -Dcom.android.traceview.toolsdir= -jar %jarpath% %*

Simply add in the argument:

call %java_exe% -Xmx1024m -Djava.ext.dirs=%javaextdirs% -Dcom.android.traceview.toolsdir= -jar %jarpath% %*

And it'll work properly.

X6PLT 
Java, "it just works".

Firefox: Just got a little bit more respect in my books :)

image

I couldn't help but smile when I saw this.

A Brief, Incomplete, and Mostly Wrong History of Programming Languages

An absolute cracker of a nerdy post by James Iry, author of One Div Zero.

See it here: A Brief, Incomplete, and Mostly Wrong History of Programming Languages

Couldn't agree more with 1996-2001 =P

Facebook Photo Upload keeps failing (fixed)

Every once in a while, FaceBook's photo uploader will constantly fail. Very rarely is it a server fault. Most of the time it would be due to my crappy Australian internet connectivity with the rest of the world.

But, if it happens over and over and restarting your browser/internet wont help, then your best bet would be to clear the Java application/applet cache.

Firstly, close Firefox/IE.

Then open up "Java" in your control panel.

In the "General" tab, click on "Settings ..." under "Temporary Internet Files". Click on the "Delete Files..." button to clear your applet cache.

Load the FaceBook "Add Photos" page and it should download the updated copy of Photo Uploader.

If it still doesn't work, wait a while and try again.

*update - 29/01/2010*

There's a new photo uploader plugin now that looks more like the Mac OS X explorer interface. It's nice, but it tries to install in 2 ways (for Firefox anyway).

  • 1. Installs a Firefox plugin via a Java applet.
  • 2. If the applet fails to load, Facebook provides a link to a 2mb installer file which installs the necessary plugins to your browsers. No restart necessary for Firefox.

It was tested a couple of weeks ago (WinXP) and it just didn't work for me. Now that its up again, its working nicely and is alot more responsive than the old photo uploader.

*update - 21/02/2010*

I tested it on Windows 7 and it failed. Damn!

I tried it again and it suddenly worked. No idea wtf happened.

Report any issues you're having to Facebook here to make sure they know about your problems.

*update - 06/04/2010*

If you're having crashing issues with the new photo uploader plugin, see here.

Setting up Eclipse for Android development

Curious to see what the Android platform has to offer, I decided to try out some basic app development with it.

What you need:
  • Android SDK
  • Java SDK (JDK)
  • Eclipse

What I got: (for this tutorial)

File Setup:
I put all my files in a common folder, just to make things easier to find.
You can put them anywhere else, just remember to adjust the pathnames in this tutorial accordingly.

Create a folder "D:\Android" and extract the contents of both "android-sdk-windows-1.1_r1.zip" and "eclipse-SDK-3.4.2-win32.zip" into it.

Then create a folder "D:\Android\workspace" to save your Eclipse projects.

The folder should now contain 3 folders:
  • D:\Android\android-sdk-windows-1.1_r1\
  • D:\Android\eclipse\
  • D:\Android\workspace\

Installing ADT Plugin for Eclipse:

Starting up "D:\Android\eclipse\eclipse.exe" will load the Eclipse IDE.
When Eclipse asks you what workspace to load up, select "D:\Android\workspace".

(optional)
Click "Use this as the default and do not ask again" if you don't plan on changing your workspace.

Once its done loading, close the welcome screen.

Go to "Help -> Software Updates -> Available Software -> Add Site".
Paste in https://dl-ssl.google.com/android/eclipse (or http://dl-ssl.google.com/android/eclipse if the other site isn't working).
Expand the new Android node and wait for it to fetch some information.
Once it loads, select "Developer Tools" and click the "Install" button.
This will take a while to finish.
Restart Eclipse when prompted.

Configuring ADT Plugin:
Once its done reloading, there is one last step in configuring Eclipse before its ready for use.
Go to "Windows -> Preferences -> Android -> Browse".
Select "D:\Android\android-sdk-windows-1.1_r1" and click OK.

You are now ready to develop with the Android platform.



Testing:
To test out your setup, right click on the package explorer and select "New -> Project"
Select "Android -> Android Project" and click "Next".

Since its easier to test with some existing code, we're going to use the sample project "LunarLander".
Click on "Create project from existing source" and then click "Browse".
Select "D:\Android\android-sdk-windows-1.1_r1\samples\LunarLander" and click "Finish".

Go to "Run -> Debug -> Android Application".

The emulator should start up and display "A N D R O I D _" on a black screen for a few moments. It'll take a little bit longer to load up the Android OS, but eventually the LunarLander application should start up.

Enjoy!
 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog