I have released a beta snapshot version of a Java wrapper for StackOverflow API at http://code.google.com/p/stackoverflow-java-sdk/. Please try it and provide your feedback.

Screenshot / Code Snippet

There are two different interfaces to the API. One is the query API which has a Builder like syntax.

    StackExchangeApiQueryFactory queryFactory = StackExchangeApiQueryFactory.newInstance("applicationKey");
    QuestionApiQuery query = queryFactory.newQuestionApiQuery();
    List<Question> questions = query.withSort(Question.SortOrder.HOT).withPaging(new Paging(1, 20)).withTimePeriod(new TimePeriod(new Date(), new Date())).withFetchOptions(EnumSet.of(FilterOption.INCLUDE_BODY, FilterOption.INCLUDE_COMMENTS)).list();

The other is a simple facade which exposes all the methods of the API.

    StackExchangeApiClientFactory factory = StackExchangeApiClientFactory.newInstance("applicationKey");
    StackExchangeApiClient client = factory.createStackOverflowApiClient();
    List<Question> questions = client.getQuestions(Question.SortOrder.HOT, new Paging(1, 20), new TimePeriod(new Date(), new Date()), EnumSet.of(FilterOption.INCLUDE_BODY, FilterOption.INCLUDE_COMMENTS));

The lists returned by both the methods are instances of PagedList which provide methods to aid in pagination of the result.

Note that by default the library uses StackOverflow as the API provider. You can change that to use anyother by.

    query.setApiProvider(new SuperUserApiProvider());
    client.setApiProvider(new SuperUserApiProvider());

The method/class names somewhat map with the names in the API. In case of confusion look in the examples directory of the distribution for more examples. I will be updating the javadoc soon.

About

stackoverflow-java-sdk is an open source library hosted at Google code. I created this library to be used in an internal project. But its comprehensive enough to be used by anyone looking to integrate with the API of StackOverflow or partner sites.

License

stackoverflow-java-sdk is open source with an Apache License 2.0.

Download

You can download the library from Google Code http://code.google.com/p/stackoverflow-java-sdk/.

Platform

Its supported for any platform on which Java is supported which include Google AppEngine and Android.

Contact

Any issues can be filed on the Google Code Issue tracking system. http://code.google.com/p/stackoverflow-java-sdk/issues/list

Code

The library is hosted at Google Code as a mercurial repository. You can create a clone and get the code and make changes to it. To contribute to the main repository you will have to create an issue with a pull request with details of your changes. The library has one dependency on Google GSON which can be downloaded from here http://code.google.com/p/google-gson/.

link|improve this question
Will this work with J2ME development? Just wonderin'... – George Edison May 25 '10 at 5:46
It uses HttpUrlConnection to communicate with the API. HttpUrlConnection is available in J2ME CDC profiles, so theoretically it should work on those platforms, though I have not tried it. So not sure. – nabeelmukhtar May 25 '10 at 6:58
1  
Might want to change the name... you're not supposed to use 'StackOverflow' in the name. – George Edison Jul 5 '10 at 19:50
You are right. The names were old and have been updated. – nabeelmukhtar Jul 7 '10 at 8:54
feedback

1 Answer

This is a nice implementation, but it doesn't work on J2ME nor Android though. You seem to be experienced Java developer because you are using the Adapter pattern design and some introspection stuff.

The main problem here is that J2ME and Android don't provide the java.beans package which is needed in order to use introspection as you are using it... even worst, the Dalvik VM avoids compiling 'core' libraries so I was not able to adapt the java.beans implementation for Android.

There are other problems, which are easier to solve, like the way you are getting the application constants values (for instance, that won't work on J2ME unless you use microproperties).

I guess someone will have to implement a wrapper for Android that uses the JSON libraries provided by the Android API. Maybe I could give it a try in some days.

I also took a look of the StackExchange Java client library; it seems to be simpler, but it uses the Java Jersey API and I haven't been able to make it run on Android...

link|improve this answer
Thanks for pointing this out. I will have to change the BaseJsonAdapter implementation to use reflection etc. for android. Let me try this. Will have to change a lot to support j2me though. – nabeelmukhtar Jun 15 '10 at 13:04
You could better start with Android since it has a richer SDK... the changes won't be dramatic. For j2me I guess it'd be better to create a miniwrapper version. – Cristian Jun 15 '10 at 13:12
2  
@Christian: If you'd like, you can test out StackWrap4J. I tested it out on the Android emulator, but I don't have a physical device to test it on yet. I'd appreciate any feedback you can give us if you decide to use it. Thanks. – Bill the Lizard Jun 21 '10 at 3:06
@Bill Thanks! I'll give it a try as soon as I can :D – Cristian Jun 30 '10 at 13:54
Try the latest version. It uses Google GSON for json parsing which works on Android as well. – nabeelmukhtar Jul 5 '10 at 12:52
feedback

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged