square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
43.08k stars 7.31k forks source link

Using Retrofit to Connect with Google App Engine Servlets #1535

Closed troy21688 closed 8 years ago

troy21688 commented 8 years ago

I have been experimenting with Google App Engine, as I want to learn the basics of server-side programming with Java.

I have successfully followed the tutorial located here: https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/HelloWorld

I was able to view the Toast message on my app, so I know that under that example, I was able to connect to the server and receive a response.

However, I understand that AsyncTask is becoming outdated, and I am trying to learn Retrofit. Basically, I am trying to replicate the Servlets example from Google in the link above using Retrofit. Below is my code, I am not connecting to the server and would hope that somebody could point me in the right direction:

MainActivity:

public class MainActivity extends AppCompatActivity {

private static String PROJECT_URL = "http://retrofit-test-1203/hello";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(PROJECT_URL)
            .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(), response.toString(), Toast.LENGTH_LONG).show();

        }

        @Override
        public void failure(RetrofitError error) {
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
        }
    });

}

}

User Service:

public interface UserService {

static String PROJECT_URL = "http://retrofit-test-1203/hello";

@POST(PROJECT_URL)
void testRequest(@Query("test") String test, 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);
}

}

JakeWharton commented 8 years ago

There's a lot going on here. We use GitHub issues for tracking specific bugs and feature requests. For user support we direct people to StackOverflow and its 'retrofit' tag since there's many more people in the community participating there and it's also a much more appropriate place for interactive help.

It looks like the problem with your above code is that you are passing the full URL as both the endpoint and the relative URL. Instead, pass only the host as the endpoint, and then use just the path /hello on the annotation.