-2

I want to make a login program in Android. I wrote the code for this login program. In this program I send username and password to the server for authentication. But when I opened the app and enter username and password and click on LogIn button my application unfortunately stopped. But username and password will sent to server. Than means my application will stopped when I click on Login button. Here is my code.

public class LogIn extends Activity implements View.OnClickListener {

    private Socket socket;
    private DataOutputStream out;
    private DataInputStream in;

    private String username,password,s;

    EditText eT_username,eT_password;
    Button b_login,b_signup,b_connect;
    TextView t;

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

        eT_username = (EditText)findViewById(R.id.eT_username);
        eT_password = (EditText)findViewById(R.id.eT_password);

        b_login = (Button)findViewById(R.id.b_login);
        b_signup = (Button)findViewById(R.id.b_signup);
        b_connect = (Button)findViewById(R.id.b_connect);

        t = (TextView)findViewById(R.id.tV);

        b_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String toast_msg;
        try {
            out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());

            out.writeUTF("1");

            username = eT_username.getText().toString();
            password = eT_password.getText().toString();

            out.writeUTF(username);
            //        out.flush();
            out.writeUTF(password);
            //        out.flush();

            t.setText("hi");

            s = in.readUTF();


            if (s.equals("ok")) {
                toast_msg = "Login is successfull";
                t.setText(toast_msg);
            } else {
                toast_msg = "username or password is incorrect";
                t.setText(toast_msg);
            }

        } catch (IOException e) {
            t.setText(e.toString());
        }
    }

    public void connect(View v)
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try
                {
                    socket = new Socket("192.168.0.101",5000);

                }
                catch(Exception e)
                {
                    TextView t = (TextView)findViewById(R.id.textView);
                    t.setText(e.toString());
                    //   Toast.makeText(LogIn.this,"Make sure that you are connected to Internet",Toast.LENGTH_LONG).show();
                }
            }
        }).start();
    }

    public void openSignUp(View v)
    {
        if (v.getId()== R.id.b_signup)
        {
            Intent i =  new Intent(LogIn.this,SignUp.class);
            startActivity(i);
        }
        new SignUp(socket);
    }
}

error:

02-17 23:34:34.340 2156-2156/? E/Zygote: v2
02-17 23:34:34.340 2156-2156/? I/libpersona: KNOX_SDCARD checking this for 10118
02-17 23:34:34.340 2156-2156/? I/libpersona: KNOX_SDCARD not a persona
02-17 23:34:34.340 2156-2156/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-G531F_5.1.1_0020
02-17 23:34:34.340 2156-2156/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
02-17 23:34:34.340 2156-2156/? I/art: Late-enabling -Xcheck:jni
02-17 23:34:34.350 2156-2156/? D/TimaKeyStoreProvider: in addTimaSignatureService
02-17 23:34:34.350 2156-2156/? D/TimaKeyStoreProvider: TimaSignature is unavailable
02-17 23:34:34.350 2156-2156/? D/ActivityThread: Added TimaKesytore provider
02-17 23:34:34.350 2156-2156/? I/SAMP: ActivityThread() - SAMP_ENABLE : true
02-17 23:34:34.450 2156-2156/? D/ContextImpl: ContextImpl running for user UserHandle{0} 0
02-17 23:34:34.470 2156-2156/kiran.onlinejavacompiler D/ContextImpl: ContextImpl running for user UserHandle{0} 0
02-17 23:34:34.470 2156-2156/kiran.onlinejavacompiler W/ResourcesManager: getTopLevelResources: null for user  0
02-17 23:34:34.490 2156-2156/kiran.onlinejavacompiler D/ContextImpl: ContextImpl running for user UserHandle{0} 0
02-17 23:34:34.500 2156-2156/kiran.onlinejavacompiler D/ContextImpl: ContextImpl running for user UserHandle{0} 0
02-17 23:34:34.500 2156-2156/kiran.onlinejavacompiler W/ResourcesManager: getTopLevelResources: null for user  0
02-17 23:34:34.510 2156-2156/kiran.onlinejavacompiler W/ResourcesManager: getTopLevelResources: null for user  0
02-17 23:34:34.510 2156-2156/kiran.onlinejavacompiler D/DisplayManager: DisplayManager()
02-17 23:34:34.560 2156-2156/kiran.onlinejavacompiler D/PhoneWindow: *FMB* installDecor mIsFloating : false
02-17 23:34:34.560 2156-2156/kiran.onlinejavacompiler D/PhoneWindow: *FMB* installDecor flags : -2139029248
02-17 23:34:34.600 2156-2199/kiran.onlinejavacompiler D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-17 23:34:34.630 2156-2156/kiran.onlinejavacompiler D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
02-17 23:34:34.630 2156-2156/kiran.onlinejavacompiler D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
02-17 23:34:34.670 2156-2199/kiran.onlinejavacompiler I/OpenGLRenderer: Initialized EGL, version 1.4
02-17 23:34:34.670 2156-2199/kiran.onlinejavacompiler D/GC: <tid=2199> OES20 ===> GC Version   : GC version rls_5011p6_GC5.5.13 
02-17 23:34:34.680 2156-2199/kiran.onlinejavacompiler D/OpenGLRenderer: Enabling debug mode 0
02-17 23:34:34.840 2156-2156/kiran.onlinejavacompiler I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3241f92a time:6983437
02-17 23:34:36.730 2156-2156/kiran.onlinejavacompiler D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
02-17 23:34:38.940 2156-2156/kiran.onlinejavacompiler D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
02-17 23:34:41.900 2156-2156/kiran.onlinejavacompiler D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
02-17 23:34:44.820 2156-2156/kiran.onlinejavacompiler D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
02-17 23:34:44.900 2156-2156/kiran.onlinejavacompiler D/AndroidRuntime: Shutting down VM
02-17 23:34:44.900 2156-2156/kiran.onlinejavacompiler E/AndroidRuntime: FATAL EXCEPTION: main Process: kiran.onlinejavacompiler, PID: 2156 android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
at libcore.io.IoBridge.recvfrom(IoBridge.java:553)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
at libcore.io.Streams.readFully(Streams.java:81)
at java.io.DataInputStream.readShort(DataInputStream.java:152)
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:165)
at java.io.DataInputStream.readUTF(DataInputStream.java:169)
at kiran.onlinejavacompiler.LogIn.onClick(LogIn.java:65)
at android.view.View.performClick(View.java:5076)
at android.view.View$PerformClick.run(View.java:20279)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
02-17 23:34:46.350 2156-2156/kiran.onlinejavacompiler I/Process: Sending signal. PID: 2156 SIG: 9
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Tejas Desai
  • 45
  • 1
  • 4

2 Answers2

0

You never call the connect() method, so socket is null in the onClick() method where you try to use it to get the output stream.

But then again fixing that won't be enough. You would instantiate socket in a separate thread and then try to actually use the socket in the main thread (the "UI thread") which would again cause your app to crash.

Maybe just place all the network operations into an AsyncTask.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
0

You probably are trying to connect in the main thread, make a thread and connect on this,

other problem in your code is the connect method, enclose the expcetion like this (Sorry for my english)

public void connect(View v)
{
    new Thread(new Runnable() {
        @Override
        public void run() {
            try
            {
                socket = new Socket("192.168.0.101",5000);

            }
            catch(Exception e)
            {

               Runneable runException = new Runnable() {
                 @Override
               public void run() {
                TextView t = (TextView)findViewById(R.id.textView);
                t.setText(e.toString());
                //   Toast.makeText(LogIn.this,"Make sure that you are    connected to Internet",Toast.LENGTH_LONG).show();
           };
        runOnUiThread(runException);
            }
        }
    }).start();
}