java - Need Help Connecting to Google App Engine With Retrofit -
i have followed google tutorial here, using android studio servlets module connect google app engine. able see toast message on device, meaning connected server , received response.
i noticed module uses asynctask handle background tasks. understand, retrofit simpler , effective method of handling tasks in background thread. trying replicate google tutorial mentioned above using retrofit 1.9.0 instead of servletpostasynctask java class provide.
below code:
mainactivity:
public class mainactivity extends appcompatactivity { //set url of server, defined in 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 new restadapter object, setting endpoint url of server restadapter restadapter = new restadapter.builder() .setendpoint(project_url) .build(); //instantiate new userservice object, , call "testrequst" method, created in interface //to interact 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 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, required 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 form post 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 name"); } resp.getwriter().println("hello " + name); } } in userservice.testrequest()method, pass in "test_name" string parameter. text hope pass server, , see toast displays "hello test_name" (after receiving server response), google app engine servlets module explains.
right now, receiving below error:
any advice on using retrofit google app engine appreciated, there limited documentation.
first of all, base url should domain of site i.e http://retrofit-test-1203.appspot.com (it should not include path resource you're trying access) declare this:
private static string project_url = "http://retrofit-test-1203.appspot.com" second, don't use base url(project_url in case) relative url in declaration of userservice interface. should use relative url here i.e "/hello" (and must start slash), this:
public interface userservice { @post("/hello") void testrequest(@query("test") string test, callback<string> cb); } 
Comments
Post a Comment