Using Watson Translation service in an Eclipse SWT application Part 2

The next follow-up to the last article is the code I used to communicate with the Watson Translation service. But first I want to show the flow of the data:

So basically the SWT client sends a JSON string to the server in the body of the message. You can do this as long as you specify application/json as the Content-Type. For JSON, I use the JSON library for Java over at json.org and its been perfect. I have even begun using this library for all of my in memory objects because then I can easily serialize it out for storage or over the network.

Below you will see the primary code to send this message:

JSONObject payload = new JSONObject();
                	
payload.put("toLang", obj.get("to"));
payload.put("text", obj.get("text"));
                	                	
URL url = new URL(pet_bluemix_translation_service);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(payload.toString());
out.close();
                         
//Now get the response
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                	
String n = in.readLine();
StringBuffer sbValue = new StringBuffer();
                	
while(n != null){
	sbValue.append(n);
	n = in.readLine();
}
in.close();
                	
final String value = sbValue.toString();
Advertisements

Finding every possible combination of array entries from multiple lists with unknown bounds in Java

I am sure this post will resonate with pretty much any developer that reads it. Have you ever had a problem where you had to really step back and think about for a long time? A problem where the wealth of the internet helps a little but just isn’t what you were looking for? One big problem is I had recursion on my brain and I was convinced for a long time recursion would be needed. Seeing many similar problems solved with recursion I thought it was the way to go.. “Much to learn you still have…my old padawan.” – Yoda.

A long time for me is hours or even days. Usually I have some level of experience in the past that helps me quickly figure out coding tasks. Well for some reason this thing got me thinking for almost an entire day. And when I say entire day I mean I still do other things but its embedded in the back of my brain. I had my notebook with me the entire day drawing out boxes, arrows, and visualizing walking through loops. It wasn’t until later that night when I realized what I had to do. This was that kind of problem.

Head shots thoughtful, thinking, finding solution man

Continue reading

Eclipse and Java in schools oh my!

juno-splashHave you ever had your child come to you with school work and feel like, “wow, I have no idea how to do that” or you have to read the chapter to remember how to do something? Well for the first time in a long time I was able to whip an answer out in seconds. I get a text from my son asking if I can help him and his friend with some college homework and he mentions they are Eclipse questions. My first thought was “wow, they are using Eclipse in school?”. And I was correct, they are using Eclipse and even programming in Java! I was pretty shocked but apparently after scouring the internet a lot of colleges are now using Java and Eclipse.

Continue reading

Sorting a HashMap in Java: SortedMap to the rescue!

java-sorting

I recently had to populate a list and make sure there were no duplicates so I immediately thought of a hash map. While this worked great, I also needed the output to be sorted alphabetically. This is where SortedMap comes into play.

While I could have done this a few ways, I found this method very simple and straightforward. The hash map provides the unique values while the SortedMap provides the auto sorting I needed. I only had about 1000 records to sort so I didn’t have to worry about performance.

Continue reading

Eclipse SWT ImageData to the rescue for image resizing!

A little side project I have here at work has taken a life on of its own. Most recently with the launch of the new Aurora storefront I once again enhanced an internal tool to automatically size product images to adhere to the size requirements for the store. I ended up using Eclipses SWT (The Standard Widget Toolkit) that comes with Eclipse to do this. What I wanted to share today was how easy this was using the SWT API’s.

Continue reading

Using the XPath API to figure out my stats

One major problem I have with base WordPress is while it does a good job giving you stats around views (the number of hits you get per post) it does not do a good job for post counts. In the previous post I had to create a chart that showed how many blog posts I wrote in the months and years past. I am sure there is a plugin out there for this so if you know of one please mention it in the comments!

Continue reading

Quick Tip Video: Getting The XPath In Google Chrome

In this video I show how easy it is to get the xPath from Google Chrome. You can use the xPath when processing the DOM tree in your code. I use the javax.xml.xpath in Java to process DOM tree; the API is very powerful and easy to use.

Why your blog isn’t getting enough hits, top 8 tips!

I just read this blog post “7 Secrets to Effective Corporate Blogging” and I think this is also a great list for the regular bloggers like myself. As a matter of fact I guess I am doing their last tip right now by “stealing their idea” and adding to it, plus one more tip! I have been blogging for just over six years and I have seen a lot of hits on some posts and not nearly as many hits on other posts that I thought would be good reads. I have learned a lot over the years in this space and hopefully some of this information will be useful to other bloggers or newbies.

The biggest success in my opinion for any blog is the consistency of the content in the blog. This blog pretty much focuses on software development in many areas, collaboration, social, commerce, programming, administration, etc. basically whatever I do in my own time and my professional time around those areas. Over the years I have gained readers and lost readers.  I am definitely not an authority or generate 10,000 hits a day, or even a thousand hits a day in most cases; I do think I get enough readers to at least talk about what I think works.

So on to my “keys to success”, first and foremost get followers on Facebook, Twitter, LinkedIn, and any other site you have presence on. This will give you an opportunity to post your blog posts there. Many may find it annoying but many also look at those social networks to get news versus using a feed reader or visiting blogs individually.

Tip 1 – I find that scheduling posts between 4am and 5am Eastern Standard time is the best for me because I have many readers in Europe. Having your blog software automatically Tweet or share the post at that time is also a good idea. You want to promote your post URL between 8am and 10am in the different time zones. Studies have shown this is when most people read news on the internet.

Tip 2 – SEO can not be underestimated! Getting your post URL on the different social networks and then getting people to reply, like, Tweet or whatever will raise your SEO score on the search sites. A good example on my site are these two posts, they each get about 5-10 hits per day and a few are years old, the Eclipse preferences one was actually posted in 2007!

Tip 3 – Blog titles, very important. This particular post might get a lot of hits just for the title alone. The content may or may not work but hey, at least the title drove something. Many bloggers use “shock” titles to get a lot of hits but then the blog itself loses credibility over time.

Tip 4 – Stay consistent to your blogs purpose. Whether you blog about software or pickles make sure you stick to those guns. I use to post a lot of personal information in the beginning and found many liked it for the occasional read but it really didn’t build my readership. When I really focused on my specialty I started gaining hits.

Tip 5 – Use images and videos! Nothing makes a point like a picture or video. I may not be the most descriptive person with words so many times I just use a video or picture to help out. It also makes the blog itself a little more flashy. I may actually use too many pictures and videos…

Tip 6 – Your blogging software and the tools to effectively promote, attract, and publish your posts. It’s no secret I use WordPress as my blogging software. I used three different blogging software for this blog and eventually settled on WordPress. I feel the administration, the plugins, the templates, the community and the quality of the code is unparalleled in the free blogging software world. You can probably find “better” software but you will most likely have to pay for it or have it hosted.

Tip 7 – Promote, Promote, Promote. You have to have social presence and you have to promote your blog. Those Tweets, comments, mentions, likes, etc all work in your favor and could gain new readers. Use hash tags in Twitter so your post shows up in searches and other peoples lists. This is how you will gain new followers and readers.

Tip 8  – Comments are key. I actually lack in this space a lot. I don’t get a lot of comments on my blog because maybe I mostly post FYI or education like material. The better blogs out there actually get engagement from their community. I am always trying to get people to comment here but what I am finding now is most people comment on the social sites like Facebook, Twitter and LinkedIn. So thank you to all who comment on this blog!

Oldy but goody article: The Java XPath API

If you Google “Java XPath” this is one of the first articles returned. Clear and concise explanation for how to use the API:

XPath expressions are much easier to write than detailed Document Object Model (DOM) navigation code. When you need to extract information from an XML document, the quickest and simplest way is to embed an XPath expression inside your Java program. Java 5 introduces the javax.xml.xpath package, an XML object-model independent library for querying documents with XPath.

via The Java XPath API.

Symantec C++

Long ago on a computer, almost two decades ago, I used the Symantec C++ compiler. I only made it through version 6 and 7 and eventually moved on to Microsoft C++. The Symantec IDE was way ahead of its time and at first using MSDEV was a struggle. Today, MSDEV is arguably one of the best IDE’s. Why am I writing about this? I was cleaning out my old software CD’s and I must have had a dozen copies of the compiler because I was on the subscription model. Made me think how far IDE’s have come over the past 20 years. Comparing that software to Eclipse is almost laughable. Looks like I should also get rid of about 30 books on software development – most of which are totally out of date, with only a few keepers.

Makes me also think what software we use today will be laughable in twenty years…