You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Sep 17, 2022. It is now read-only.
I am trying to replicate the Google App Engine Servlets module [here][1] 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][2] in the issues of the GitHub repository.
As a result, I am using Retrofit 1.9 and OkHttp 2.3 dependencies.
```
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();
}
});
}
});
}
}
```
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.
The text was updated successfully, but these errors were encountered:
I think the correct forums for this would be someone more familiar with retrofit and how it might handle toast creation or other problems... However to help you debug you can look at your server side logs. Perhaps that will give you some insight into whether you are even sending a request. Check the google developer console logging to see what requests are coming in.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I made a previous post and appear to have made progress, but please see the StackOverflow post for reference: http://stackoverflow.com/questions/35068601/how-to-handle-jsonsyntaxexception-when-receiving-response-from-gae
I am trying to replicate the Google App Engine Servlets module [here][1] 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][2] 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:
MainActivity:
UserService (Retrofit)
My Servlet:
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.
The text was updated successfully, but these errors were encountered: