Tutorial: Publishing HITs with MTurk and the Java programming language

Amazon Mechanical Turk
Happenings at MTurk
4 min readMay 28, 2018

--

In a previous blog post, we shared a tutorial on how to get started with Amazon Mechanical Turk (MTurk) and the Java programming language. This blog post builds on that tutorial to show how to create Human Intelligence Tasks (HITs) in MTurk using the Java programming language and MTurk’s Application Programming Interface (API).

If you didn’t already step through the previous tutorial, we suggest you start there. You can find it here: https://blog.mturk.com/tutorial-using-the-mturk-api-with-the-aws-sdk-for-java-48b4826f9122

Quick recap of where we left off

In the previous tutorial, we installed Eclipse and configured it to work with the AWS Software Development Kit (SDK) for Java using the AWS Toolkit for Eclipse. When we left off in the previous tutorial, our code called MTurk’s API to invoke the GetAccountBalance method. It looked a little like this:

Now, let’s create some HITs

In today’s tutorial, we’re going to build on the setup work we did last time, but this time we’ll create some HITs in MTurk. To do this, we’ll be calling the CreateHIT API operation.

To start, we’ll leverage the sample code found here:

https://github.com/aws-samples/mturk-code-samples/blob/master/Java/CreateHITSample.java

Begin by creating a file called CreateHITSample.java in your existing project. You can do this by selecting the src/main/java package, then click the right mouse button on(default package) and select New > Class. It should look like this:

A window should appear. The only field you should need to fill in is the Name: where you’ll enter CreateHITSample then click Finish. It should look like this:

Next, paste the code found here:

https://github.com/aws-samples/mturk-code-samples/blob/master/Java/CreateHITSample.java

into the CreateHITSample.java file, replacing any content already in the CreateHITSample.java file. It should look like this once you’re finished.

This code calls MTurk’s CreateHIT API to construct a HIT. The question for the HIT is contained in a file called my_question.xml that we’re going to add to our project in a moment. But if you were to try and run the code without that file, you’ll get an error message that looks like this:

To address this error, we’ll download the my_question.xml file that the code is looking for you. You can find this file here:

https://github.com/aws-samples/mturk-code-samples/blob/master/Java/my_question.xml

To add this file to your project, click the right mouse button on the project mturk-app then select New > File. It should look like this:

A dialog box should appear. In the File name: field, type my_question.xml then press Finish. It should look like this:

Eclipse will create the my_question.xml file and open it. By default, it will be opened in “Design” mode, which can be more convenient if we’re building our XML elements one-by-one.

Since we already have a question file, we’re going to click the Source tab so that we can paste in our XML. Then, paste the contents of the my_question.xml that we downloaded above. It should look like this:

Drumroll, please….

Now we’re going to create the HIT. To do this, simply Run your CreateHITSample.java file again (if you’ve already run it above, you should simply be able to click on the Run menu and select Run from the options). When you run the CreateHITSample file, you should see the following:

And if you open the URL that’s shown in the Output Console, you should see the following:

Congratulations! You’ve just created your first HIT with the MTurk API using the AWS SDK. Nice work.

Wrapping up

Today, we built on our previous Java tutorial to the create HITs for Workers using the MTurk Requester API. In future tutorials, we’ll build on this to show readers how to do even more. In the meantime, you can learn more about the MTurk API by accessing the MTurk API documentation here. You can also check out more Java sample code for the AWS SDK and MTurk API here.

We hope you enjoyed today’s tutorial. If you have any questions, please post a question to our MTurk forums. To become a Requester, sign up here. Want to contribute as a Worker customer? Get started here.

--

--