java - Error 404 consuming Google Cloud Endpoint -
i have google cloud endpoint created android studio.
i testing consume locally backend endpoint using http server , client running android studio in pc.
using google cloud endpoints api explorer, can execute resource "doregister" without problem.
but when try consume cloud backend in app, 404 error in http request. know can use classes generated android studio consume backend, i´d use http.
i using serverurl = "http://10.0.2.2:8080/_ah/api/contactendpoint/v1/doregister"; don´t know if correct.
please, me doing wrong?
server backend: @apimethod(name = "doregister") public contact doregister(@named("from") string from, @named("reg_id") string reg_id) { contact contact = findrecordbyemail(from); if (contact == null) { contact = new contact(from, reg_id); try { contact = this.insertcontact(contact); } catch (conflictexception e) { logger.log(level.severe, e.getmessage()); //return null; } } else { contact.setregid(reg_id); } logger.log(level.warning, "registered: " + contact.getid()); return contact; } client backend: public static void register(final string email, final string regid) { string serverurl = "http://10.0.2.2:8080/_ah/api/contactendpoint/v1/doregister"; map<string, string> params = new hashmap<string, string>(); params.put(common.from, email); params.put(common.reg_id, regid); try { post(serverurl, params); } catch (ioexception e) { } } private static void post(string endpoint, map<string, string> params) throws ioexception { url url; try { url = new url(endpoint); } catch (malformedurlexception e) { throw new illegalargumentexception("invalid url: " + endpoint); } stringbuilder bodybuilder = new stringbuilder(); iterator<entry<string, string>> iterator = params.entryset().iterator(); // constructs post body using parameters while (iterator.hasnext()) { entry<string, string> param = iterator.next(); bodybuilder.append(param.getkey()).append('=').append(param.getvalue()); if (iterator.hasnext()) { bodybuilder.append('&'); } } string body = bodybuilder.tostring(); //log.v(tag, "posting '" + body + "' " + url); byte[] bytes = body.getbytes(); httpurlconnection conn = null; try { conn = (httpurlconnection) url.openconnection(); conn.setdooutput(true); conn.setusecaches(false); conn.setfixedlengthstreamingmode(bytes.length); conn.setrequestmethod("post"); conn.setrequestproperty("content-type", "application/x-www-form-urlencoded;charset=utf-8"); // post request outputstream out = conn.getoutputstream(); out.write(bytes); out.close(); // handle response int status = conn.getresponsecode(); if (status != 200) { throw new ioexception("post failed error code " + status); } } { if (conn != null) { conn.disconnect(); } } }
you need find pc local ip address , replace 10.0.2.2 it.
try google how find pc local ip address. this article looks promising
Comments
Post a Comment