android - Using OkHttp library for posting to a PHP script that saves to MySQL -


i writing code registers new user server. in order so, implemented post request using okhttp library.

public class registrationmanager {     private final string tag_register = registrationmanager.class.getsimplename();     private okhttpclient client = new okhttpclient();     private final string registrationurl = url.getinstance().geturlregister();      public void registeruser(final user newuser) {         requestbody body = new formencodingbuilder()                 .add("email", newuser.getemail())                 .add("name", newuser.getname())                 .add("password", newuser.getpassword())                 .add("birthday", newuser.getbirthday())                 .build();          request request = new request.builder().url(registrationurl).post(body).build();          call call = client.newcall(request);          call.enqueue(new callback() {              @override             public void onfailure(request request, ioexception e) {                 log.e(tag_register, "registration error: " + e.getmessage());             }              @override             public void onresponse(response response) throws ioexception {                  try {                     string resp = response.body().string();                     log.v(tag_register, resp);                     if(response.issuccessful()) {                      } else {                      }                 } catch(ioexception e) {                     log.e(tag_register, "exception caught: ", e);                 }             }         });     } } 

when enter user information (email, name, password, birthday) , press register button on activity, should send request body server (which developed in php) should receive , store user data mysql database, keeps failing so. how should modify code user data stored in mysql database?

(edited)

the code below php part.

<?php  if (isset($_post['tag']) && $_post['tag'] != '') {     // tag     $tag = $_post['tag'];      // include db handler     require_once 'include/db_functions.php';     $db = new db_functions();      // response array     $response = array("tag" => $tag, "error" => false);      // check tag type     if ($tag == 'login') {         // request type check login         $email = $_post['email'];         $password = $_post['password'];          // check user         $user = $db->getuserbyemailandpassword($email, $password);         if ($user != false) {             // user found             $response["error"] = false;             $response["uid"] = $user["unique_id"];             $response["user"]["name"] = $user["name"];             $response["user"]["email"] = $user["email"];             $response["user"]["birthday"] = $user["birthday"];             $response["user"]["created_at"] = $user["created_at"];             $response["user"]["updated_at"] = $user["updated_at"];             echo json_encode($response);         } else {             // user not found             // echo json error = 1             $response["error"] = true;             $response["error_msg"] = "incorrect email or password!";             echo json_encode($response);         }     } else if ($tag == 'register') {         // request type register new user         $name = $_post['name'];         $email = $_post['email'];         $password = $_post['password'];         $birthday = $_post['birthday'];          // check if user existed         if ($db->isuserexisted($email)) {             // user existed - error response             $response["error"] = true;             $response["error_msg"] = "user exists";             echo json_encode($response);         } else {             // store user             $user = $db->storeuser($name, $email, $password, $birthday);             if ($user) {                 // user stored                 $response["error"] = false;                 $response["uid"] = $user["unique_id"];                 $response["user"]["name"] = $user["name"];                 $response["user"]["email"] = $user["email"];                 $response["user"]["birthday"] = $user["birthday"];                 $response["user"]["created_at"] = $user["created_at"];                 $response["user"]["updated_at"] = $user["updated_at"];                 echo json_encode($response);             } else {                 // user failed store                 $response["error"] = true;                 $response["error_msg"] = "error occured in registartion";                 echo json_encode($response);             }         }     } else {         // user failed store         $response["error"] = true;         $response["error_msg"] = "unknow 'tag' value. should either 'login' or 'register'";         echo json_encode($response);     } } else {     $response["error"] = true;     $response["error_msg"] = "required parameter 'tag' missing!";     echo json_encode($response); } ?> 

if need further information, please let me know time. eager find out problem is, , solve it.

check ip addrress, if using simulator, use one: make sure trapping user inputs on button click, shown here below:

mainactivitiy.java 

`package com.example.abdimuna.munokhttp;

import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.edittext;  import com.squareup.okhttp.call; import com.squareup.okhttp.callback; import com.squareup.okhttp.formencodingbuilder; import com.squareup.okhttp.okhttpclient; import com.squareup.okhttp.request; import com.squareup.okhttp.requestbody; import com.squareup.okhttp.response;  import java.io.ioexception;  public class mainactivity extends appcompatactivity {     private static final string base_url = "http://10.0.2.2/tcdc/check2.php";     private okhttpclient client = new okhttpclient();     edittext usernametextedit, passwordtextedit;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         //         usernametextedit = (edittext) findviewbyid(r.id.usernametext);         passwordtextedit = (edittext) findviewbyid(r.id.passwordtext);         button login = (button) findviewbyid(r.id.loginbutton);         login.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 // handle login                 string username = usernametextedit.gettext().tostring();                 string password = passwordtextedit.gettext().tostring();                 registeruser(username, password);             }         });     }      public void registeruser(string username, string password) {         requestbody body = new formencodingbuilder()                 .add("username", username)                 .add("password", password)                 .build();         request request = new request.builder().url(base_url).post(body).build();         call call = client.newcall(request);         call.enqueue(new callback() {              @override             public void onfailure(request request, ioexception e) {                 //  log.e(tag_register, "registration error: " + e.getmessage());                 system.out.println("registration error" + e.getmessage());             }              @override             public void onresponse(response response) throws ioexception {                 try {                     string resp = response.body().string(); //                    log.v(tag_register, resp);                     system.out.println(resp);                     if (response.issuccessful()) {                     } else {                      }                 } catch (ioexception e) {                     // log.e(tag_register, "exception caught: ", e);                     system.out.println("exception caught" + e.getmessage());                 }             }         });     }       @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     } } 

`

here main_activity.xml

        <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="username"             android:id="@+id/textview"             android:textcolor="#ff7459"             android:textsize="25dp"             android:layout_margintop="42dp"             android:layout_below="@+id/textview"             android:layout_centerhorizontal="true" />          <edittext             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/usernametext"             android:hint="username"             android:focusable="true"             android:textcolorhighlight="#ff7eff15"             android:textcolorhint="#ffff25e6"             android:layout_margintop="46dp"             android:singleline="true"             android:layout_below="@+id/imageview"             android:layout_alignparentleft="true"             android:layout_alignparentstart="true"             android:layout_alignparentright="true"             android:layout_alignparentend="true" />          <imageview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/imageview"             android:layout_below="@+id/textview"             android:layout_centerhorizontal="true" />          <edittext             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:inputtype="textpassword"             android:ems="10"             android:id="@+id/passwordtext"             android:singleline="true"             android:layout_below="@+id/edittext"             android:layout_alignparentleft="true"             android:layout_alignparentstart="true"             android:layout_alignright="@+id/edittext"             android:layout_alignend="@+id/edittext"             android:textcolorhint="#ffff299f"             android:hint="password" />          <button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="login"             android:id="@+id/loginbutton"             android:layout_alignparentleft="true"             android:layout_alignparentstart="true"             android:layout_alignparentright="true"             android:layout_alignparentend="true"             android:layout_below="@+id/edittext2" />          <button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="register"             android:id="@+id/button2"             android:layout_alignparentleft="true"             android:layout_alignparentstart="true"             android:layout_below="@+id/button"             android:layout_alignparentright="true"             android:layout_alignparentend="true" />     </tablelayout> </scrollview> 

` class test{
function test_me(){
$username = $_post['username'];
//echo $_post;
var_dump($_post);
}
}

// calling class
$test = new test();
$test->test_me();

?> `


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -