I am trying to replicate the Google App Engine Servlets module here using Retrofit instead of AsyncTask.
I find it odd, but apparently Retrofit 2.0 does not support connections to Google App Engine, as stated here in the issues of the GitHub repository.
As a result, I am using Retrofit 1.9 and OkHttp 2.3 dependencies.
I have created a project called "Retrofit Test" in the Google Developer Console, and Google has supplied me with a URL for the project: "http://retrofit-test-1203.appspot.com" with the subdomain as "http://retrofit-test-1203.appspot.com/hello. These will be my respective URL's for Retrofit. Below is my code:
Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.troychuinard.retrofittest"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp:2.3.0'
}
MainActivity:
public class MainActivity extends AppCompatActivity {
//set the URL of the server, as defined in the Google Servlets Module Documentation
private static String PROJECT_URL = "http://retrofit-test-1203.appspot.com";
private Button mTestButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mTestButton = (Button) findViewById(R.id.test_button);
//Instantiate a new UserService object, and call the "testRequst" method, created in the interface
//to interact with the server
mTestButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Instantiate a new RestAdapter Object, setting the endpoint as the URL of the server
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(PROJECT_URL)
.setClient(new OkClient(new OkHttpClient()))
.build();
UserService userService = restAdapter.create(UserService.class);
userService.testRequest("Test_Name", new Callback<String>() {
@Override
public void success(String s, Response response) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
});
}
}
UserService (Retrofit)
public interface UserService {
@POST("/hello")
void testRequest(@Query("name") String name, Callback<String> cb);
}
My Servlet:
public class MyServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Please use the form to POST to this url");
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String name = req.getParameter("name");
resp.setContentType("text/plain");
if(name == null) {
resp.getWriter().println("Please enter a name");
}
resp.getWriter().println("Hello " + name);
}
}
As you can see, I have set the project up so that I make a server request on a button click. Similar to the Google App Engine Servlets module, I am expecting to receive a response from the Server, which I then display as a Toast that says "Hello + (whatever parameter entered into the .testRequest() method, which is "Test_Name" in this case. I am receiving the error below, any help/advice on my methodology is appreciated: