android - Retrofit - Calling REST services with POST method -


i stuck first using of retrofit2 library callin rest api service post method.

i have tried learn tutorial created prabeesh (youtube). seems logic of apiinterface, apiclient same, code not working.

problem tutorial shown method, , i'm not pretty sure whether analogic interpretation post method correct..

calling service webbrowser (get method) succesfull , user created. api rest client showing successfull response (post method):

content-length: 56 content-type: text/html; charset=utf-8 date: mon, 21 aug 2017 08:18:51 gmt server: apache/2.4.23 (win64) php/5.6.25 x-powered-by: php/5.6.25 

for request on rest service created on localhost using php:

http://localhost/forman/forman/register/newuser/newsurname/geywh@test.com/password/cze/2017-07-11/1/1/2017-07-12%2000:00:00.000000/2017-07-12%2000:00:00.000000 

method createuser in index.php should called on back-end service:

$app = new \slim\app;  $app->post('/register/{first_name}/{sur_name}/{email}/{password} /{country}/{birthday}/{fav_driver}/{fav_team}/{created}/{last_login}', function (request $request, response $response) { $first_name = $request->getattribute('first_name'); $sur_name = $request->getattribute('sur_name'); $email = $request->getattribute('email'); $password = $request->getattribute('password'); $country = $request->getattribute('country'); $birthday = $request->getattribute('birthday'); $fav_driver = $request->getattribute('fav_driver'); $fav_team = $request->getattribute('fav_team'); $created = $request->getattribute('created'); $last_login = $request->getattribute('last_login');   $db = new dbhandler(); $res = $db->createuser($first_name, $sur_name, $email, $password, $country, $birthday, $fav_driver, $fav_team, $created, $last_login);          if ($res == user_created_successfully) {             $response->getbody()->write("yep, user added!!!!, $first_name $sur_name");         } else if ($res == user_create_failed) {             $response->getbody()->write("error!!!, $first_name $sur_name");         }   $response->getbody()->write("hello, $first_name $sur_name"); return $response; }); $app->run(); ?> 

so, built api interface post method calling /register site defined in .htaccess:

public interface apiinterface { @post("/register") call<user> createuser(@body user user);} 

i using user class constructor, getters, setters , serialized fields on rest service..

public class user { //constructor public user(string first_name, string sur_name, string email, string password, string country, string birthday, int fav_driver, int fav_team, string created, string last_login) { } @serializedname("first_name") public string first_name; @serializedname("sur_name") public string sur_name; @serializedname("email") public string email; @serializedname("password") public string password; @serializedname("country") public string country; @serializedname("birthday") public string birthday; @serializedname("fav_driver") public integer fav_driver; @serializedname("fav_team") public integer fav_team; @serializedname("created") public string created; @serializedname("last_login") public string last_login;  // setters public void setfirst_name(string first_name) {     this.first_name = first_name; } public void setsur_name(string sur_name) {     this.sur_name = sur_name; } public void setemail(string email) {     this.email = email; } public void setpassword(string password) {     this.password = password; } public void setcountry(string country) {     this.country = country; } public void setbirthday(string birthday) {     this.birthday = birthday; } public void setfav_driver(integer fav_driver) {     this.fav_driver = fav_driver; } public void setfav_team(integer fav_team) {     this.fav_team = fav_team; } public void setcreated(string created) {     this.created = created; } public void setlast_login(string last_login) {     this.last_login = last_login; }  // getters public string getfirst_name() {     return first_name; } public string getsur_name() {     return sur_name; } public string getemail() {     return email; } public string getpassword() {     return password; } public string getcountry() {     return country; } public string getbirthday() {     return birthday; } public integer getfav_driver() {     return fav_driver; } public integer getfav_team() {     return fav_team; } public string getcreated() {     return created; } public string getlast_login() {     return last_login; } } 

api client creating retrofit instance:

public class apiclient { public static final string base_url = "http://localhost/forman/forman/"; public static retrofit retrofit = null;  public static retrofit getapiclient()  {      if(retrofit==null) {          retrofit = new retrofit.builder().baseurl(base_url).addconverterfactory(gsonconverterfactory.create()).build();      }     return retrofit; } } 

and call api interface , api client during onclick method in code:

public void addbuttonclicklistener() {     final context context = this;      button confirm = (button) findviewbyid(r.id.bt_save_reg);     confirm.setonclicklistener(new onclicklistener() {          public void onclick(view v) {                textview firstname = (textview) findviewbyid(r.id.edittext2);             string first_name = (string) firstname.gettext().tostring();              textview surname = (textview) findviewbyid(r.id.edittext3);             string sur_name = (string) surname.gettext().tostring();              textview email = (textview) findviewbyid(r.id.reg_et_email);             string e_mail = (string) email.gettext().tostring();              textview password = (textview) findviewbyid(r.id.etpassword);             string pass = (string) email.gettext().tostring();              textview country_reg = (textview) findviewbyid(r.id.reg_tv_country);             string country = (string) country_reg.gettext().tostring();              textview birthday_reg = (textview) findviewbyid(r.id.birthday_reg);             string birthday = (string) birthday_reg.gettext().tostring();  //                textview fav_driver = (textview) findviewbyid(r.id.textview4); //                integer favdriver = integer.valueof((string) fav_driver.gettext().tostring()); // //                textview fav_team = (textview) findviewbyid(r.id.textviewfavteam); //                integer favteam = integer.valueof((string) fav_team.gettext().tostring());              user user = new user(first_name, sur_name, e_mail, pass, country, birthday, 1, 1, "2017-07-12 00:00:00.000000", "2017-07-12 00:00:00.000000");              final apiinterface apiinterface;              apiinterface = apiclient.getapiclient().create(apiinterface.class);              call<user> call = apiinterface.createuser(user);              call.enqueue(new callback<user>() {                 @override                 public void onresponse(call<user> call, response<user> response) {                      log.d("creation", "check this, because ,    onfailure never happened!");                  }                  @override                 public void onfailure(call<user> call, throwable throwable) {                  }             });           }      });  } 

sorry, plenty of code, bude novice in android, java, retrofit.. so, not pretty sure in part of code. appreciate or put bullet head :)

next time, learn on simple model couple of field only..

thank checking problem,

have nice day, jiri

try remove slash in retrofit annotation. base url has slash in end. result trying send data to

http://localhost/forman/forman//register 

the current code

@post("/register") 

the new code

@post("register") 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -