1

I'm trying to create a simple login with android via Codeigniter (I just Learn it). When I run it in android emulator (from android studio). The form showing up but when i input and press Login this message showing up

My Simple Login Keeps Stoping

this from my event log

9:03:21 PM Executing tasks: [:app:assembleDebug] 9:03:22 PM Gradle

build finished in 904ms

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.boby.myapplication, PID: 13020 android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74) at java.net.InetAddress.getAllByName(InetAddress.java:752) at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29) at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187) at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156) at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98) at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:257) at com.example.boby.myapplication.MainActivity.goLogin(MainActivity.java:72) at com.example.boby.myapplication.MainActivity$1.onClick(MainActivity.java:49) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Here is my script

package com.example.boby.myapplication;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button Login = (Button) findViewById(R.id.buttonLogin);
        final EditText username = (EditText) findViewById(R.id.UsernameID);
        final EditText password = (EditText) findViewById(R.id.PasswordID);



        Login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                final Button Login = (Button) findViewById(R.id.buttonLogin);
                final EditText username = (EditText) findViewById(R.id.UsernameID);
                final EditText password = (EditText) findViewById(R.id.PasswordID);

                String usernametext = username.getText().toString();
                String passwordtext = password.getText().toString();

                Context context = getApplicationContext();
                String text = "";

                if(usernametext.equals("") || passwordtext.equals(""))
                {
                    text = "All Field required !!!";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                }else{
                    goLogin(usernametext,passwordtext);
                }

            }
        });

    }

    protected  void goLogin(String username, String password){
        HttpURLConnection connection;
        OutputStreamWriter request = null;
        String parameters = "username="+username+"&password="+password;
        URL url = null;
        int duration = Toast.LENGTH_SHORT;
         String response;
        try{

            url = new URL("http://localhost/api/login/proses");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestMethod("POST");

            request = new OutputStreamWriter(connection.getOutputStream());
            request.write(parameters);
            request.flush();
            request.close();
            String line = "";
            InputStreamReader isr = new InputStreamReader(connection.getInputStream());
            BufferedReader reader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }

            response = sb.toString();
            Toast toast = Toast.makeText(this, response, duration);
            toast.show();
            isr.close();
            reader.close();

        }
        catch(IOException e)
        {

            Toast toast = Toast.makeText(this, "Error \n"+e, duration);
            toast.show();
        }
    }



}

and here is my Codeigniter

class Login extends CI_Controller {

    public function proses()
    {
        $username = $this->input->post("username");
        $password = $this->input->post("password");

        $result = $this->Modelmodel->check($username,$password);
        json_encode($result);
    }
}

class Modelmodel extends CI_Model {

    public function check($username,$password)
    {
        return $this->db->query("select * from user where username = '".$username."' and password = '".$password."' ")->row();
    }
}

sorry for my bad english

YVS1102
  • 2,658
  • 5
  • 34
  • 63
  • Learn how to [debug your app](https://developer.android.com/studio/debug/index.html). The message you posted has nothing to do with the actual problem. – simon Nov 29 '16 at 14:23
  • And from a brief look: You can't perform network operations on the main thread. You need to use an [AsyncTask](https://developer.android.com/reference/android/os/AsyncTask.html). See [this question](http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception) for more information – simon Nov 29 '16 at 14:25
  • thanks, now i see the error(s) – YVS1102 Nov 29 '16 at 14:36

0 Answers0