Android read file from URL

How to read a file from the internet

While coding y next app for Android, I found out that I needed to read files from URL locations and saven them on my Android device.
I searched for the first sample and put that code to work in my app. That was a quick disappointment. It dit not work and it gave me the message:

  java.io.IOException: Server returned HTTP response code: 401 for URL
  DefaultRequestDirector(5824): Authentication error: Unable to respond to any of these challenges: {}

I did not know what to make of this, since my file on the net is not protected by authentication. A lot of searching on the web did not reveal any solution to my problem (only solutions where you had to authenticate).

After long hours of searching I found a workaround for my problem. Just send in some dummy authentication and it will work.

HTTPClient / UrlConnection example

Here is my sample of my workaround

      URI _url = new URI(location);
      HttpGet httpget = new HttpGet(_url);
      DefaultHttpClient httpclient = new DefaultHttpClient();
      httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin","admin"));
      String _generatedFilename = generateFilename(_filename, savedconfiglist);
      HttpResponse response = httpclient.execute(httpget);
      HttpEntity entity = response.getEntity();
      if (response.getStatusLine().getStatusCode()==200) {
          saveArmyConfig(_generatedFilename, entity.getContent());
      } else {
        Log.d("TESTING", "error receiving content");
      }
    } catch (IOException e) {
      e.printStackTrace();
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }

Kind regards,
Marc

Posted in Android, Java | Tagged , , , , , | Leave a comment

Android XML parsing workaround

The problem

I came across a problem while using XML in my android application. When trying to parse XML in android 2.2 and above, I read the qName variable and use that to do my magic. But in sdk version 7 (2.1) this poses a serious problem. A bug in the Android code, causes the qName to be empty and the localname to be filled with your XML element value.
Continue reading »

Posted in Android, Java | Tagged , , , | Leave a comment

Android Form Field Validation

Validation ??

After coding a while for the Android platform, I was getting annoyed that there was no default validation support (i.e that I could find) for the validation of EditText, CheckBox etc fields.
Therefore I have written a small set of interfaces/Classes that gives you and me the possibility to do the validation you need.
Continue reading »

Posted in Android, Interaction, Java, Validation | Tagged , , | 2 Comments

JEE Webservice basic auth security check with Glassfish

No @RolesAllowed available for Webservices

The JEE specification is growing into a nice and easy to use specification. But some features, like Webservices and RolesAllowed, are not good enough yet. To be more precise. @RolesAllowed is not supported for plain Webservices (not doubling as EJB’s) until version 3.0 in the Servlet Specification.

After much reading on this subject, I have come to the conclusion that a pragmatic solution is required. My solution is just to inspect the principals based upon the logged in user and check myself if the user is allowed to continue. But you can also define the webservice as EJB.
Continue reading »

Posted in Java, JEE, Webservices | Tagged , , , , | Leave a comment

Flex: Dynamic binding using a mapping file

Introduction

Binding can be an time consuming task in Flex. If you have a domain model and the DTO’s for your components, you need an easy way to bind the two together. Here you have two options. The first is to write alot of BindingUtils statements in either models. And finally you can write a mapping file in xml, create a custom binder class which uses the mapping file and bind the two models together.

I have chosen the last option, because this gives me some more flexibility handling the bindings.
Continue reading »

Posted in Actionscript, Adobe, Flex 3.0 | Tagged , , , , , , | Leave a comment

Flex 3 Custom validation of grouped input fields

Introduction

Flex 3 has nicely build-in validation mechanism, but sometimes you wish they had not embedded this so deep into the components, that customization becomes a laborish task. I have also read many articles on customizations of this mechanism, but I have not found a working solution to my specific wish. What I want is validation of single input fields, multiple input and arbitrary placement of errors
besides the tooltip method.

Continue reading »

Posted in Actionscript, Adobe, Flex 3.0, Interaction | Tagged , , , , , | 3 Comments

How To: Load a spring application from a jar file

Consider the following situation:

Loading 3 application context files in a Swing application. 2 of the context files are in jar-files.
All context files depend on the component-scan option. The Application context gets loaded from my Swing app.
The method I use, is the import resource option in the context file. Read on how to configure this in your application context.
Continue reading »

Posted in Java, Spring | Tagged , , , , | Leave a comment

Webservices with Spring and Castor

Today, webservices are used primarily to expose some services of an application to the outside world. There are a lot of tools available to consume webservices. Tools like Mule, Axis and Spring.
This article describes a method of exposing Java services as webservices. The technique’s / tools used to accomplice this are; Java, Spring and Castor.
There are many other tools available, like Axis, to expose java services as webservices. The reason I used Spring and Castor is the simplicity in creating webservices. Spring takes care of all boilerplate coding and Castor’s mapping capability takes care of the Object <-> XML mapping without the need to generate any java objects the way Axis does. This way you have clean java code, supported with configuration in Spring and a clean Castor mapping to expose your service as a webservice.
Continue reading »

Posted in Castor, Eclipse, Java, Maven, Spring, Tools | Tagged , , , | Leave a comment

Easy JavaScript client-side form validation

When you are developing a website or an application, there will come a moment that you have to start using Javascript to validate your HTML forms. Here you have 4 options. Option one is not to validate. Although tempting, it is not the best option to use. Option two is to depend on your application framework and hope they have implemented client-side validation into it besides the server-side validation. Option three is to custom code some basic validation scripting yourself.
This is a great way to learn more about Javascript, but also the figurative pain in the butt. Option four and final option is to grab a existing Javascript validation framework from the Net and use it. I would recommend option four which brings me to the actual goal of my post

Many Javascript validation frameworks that are available have one thing in common. You MUST code Javascript to use it. And this is just the thing you are trying to avoid. Spry is a nice example here. Very powerful framework, but you have to know about Javascript to use it. Would it not be nice that u can use a Javascript validation framework without having to know Javascript. I know some designers would love it, since they do not have to depend on other developers for the Javascript. On the other side. I as a developer can spend my time on other issues besides validation coding. That mean that I will have more time if I provide my designer with a non intrusive way to implement client-side Javascript validation.

Continue reading »

Posted in Javascript | Tagged , , | 3 Comments