"using okhttp library for posting to a php script that saves to mysql" Code Answer

2

check ip addrress, if you are using a simulator, use this one: also make sure you are trapping user inputs on button click, like 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 the menu; this adds items to the action bar if it is present.
        getmenuinflater().inflate(r.menu.menu_main, menu);
        return true;
    }

    @override
    public boolean onoptionsitemselected(menuitem item) {
        // handle action bar item clicks here. the action bar will
        // automatically handle clicks on the home/up button, so long
        // as you specify a parent activity in androidmanifest.xml.
        int id = item.getitemid();

        //noinspection simplifiableifstatement
        if (id == r.id.action_settings) {
            return true;
        }

        return super.onoptionsitemselected(item);
    }
}

`

here is 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 the class
$test = new test();
$test->test_me();

?> `

By Henrik P. Hessel on March 7 2022

Answers related to “using okhttp library for posting to a php script that saves to mysql”

Only authorized users can answer the Search term. Please sign in first, or register a free account.