I have successfully followed the Google tutorial here
<https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/HelloWorld>,
using
the Android Studio Servlets module to connect to Google App Engine. I was
able to see the Toast message on my device, meaning I successfully
connected to the server and received a response.
I noticed that this module uses AsyncTask to handle the background tasks.
>From what I understand, Retrofit is a much simpler and effective method of
handling tasks in the background thread. I am basically trying to replicate
the Google Tutorial mentioned above using Retrofit 1.9.0 instead of the
ServletPostAsyncTask Java class that they provide.
Below is my code:
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/hello";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Instantiate a new RestAdapter Object, setting the endpoint as the
URL of the server
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(PROJECT_URL)
.build();
//Instantiate a new UserService object, and call the "testRequst"
method, created in the interface
//to interact with the server
UserService userService = restAdapter.create(UserService.class);
userService.testRequest("Test_Name", new Callback<String>() {
@Override
public void success(String s, Response response) {
Toast.makeText(getApplicationContext(),
response.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), error.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
}
UserService Interface, required by Retrofit:
public interface UserService {
static String PROJECT_URL =
"http://retrofit-test-1203.appspot.com/hello";
@POST(PROJECT_URL)
void testRequest(@Query("test") String test, Callback<String> cb);
}
My Servlet, as required by the Google Servlets Module:
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);
}
}
In my userService.testRequest()method, I pass in "Test_Name" as the string
parameter. This text is what I hope to pass to the server, and then see a
toast that displays "Hello Test_Name" (after receiving a server response),
just like the Google App Engine Servlets module explains.
Right now, I am receiving the below error:
<https://lh3.googleusercontent.com/-3vUgcFs77AI/Vql-ZDXDv-I/AAAAAAAABhA/LMr_WLeKEcc/s1600/Screenshot_2016-01-27-12-24-50.png>
Any advice on using Retrofit with Google App Engine is appreciated, as
there is limited documentation.
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/4d49e0c8-fae1-4163-b36e-5229ce42e6d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.