-1

I'm having a problem with my android login functionality, getting and i am using android device 4.1.... When i run my application it terminate immediately and console show error :

Below is my code : Please advice

public class Signup extends Activity implements OnClickListener {
// IP address where to send data
String ServerIP = "192.168.1.51";   
EditText ed_employee_username,ed_employee_password;
EditText ed_new_username,ed_new_password,ed_confirm_password;
Button btn_signup,btn_cancle_signup;
String e_username,e_password,n_username,n_password,c_password,blank = "";
String start,code,end,data;
Socket client,clientSocket;
PrintWriter printwriter;
DB_Adapter database;
ServerSocket serverSocket;
InputStreamReader inputStreamReader;
BufferedReader bufferedReader;
String employee_username_padd,employee_password_padd,IMEI,msg,result,username;
Intent i;
    @Override
      // Used for Oncreate method
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        ed_employee_username = (EditText)findViewById(R.id.ed_employee_username);
        ed_employee_password = (EditText)findViewById(R.id.ed_employee_password);
        ed_new_username = (EditText)findViewById(R.id.ed_new_username);
        ed_new_password = (EditText)findViewById(R.id.ed_new_password);
        ed_confirm_password = (EditText)findViewById(R.id.ed_confirm_password);
        database = new DB_Adapter(getApplicationContext(), "WBS", null, 1);
        btn_signup = (Button)findViewById(R.id.btn_signup);
        btn_cancle_signup = (Button)findViewById(R.id.btn_cancle_signup);

        btn_signup.setOnClickListener(this);
        btn_cancle_signup.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.signup, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
            case R.id.btn_signup:
                e_username = ed_employee_username.getText().toString();
                e_password = ed_employee_password.getText().toString();
                n_username = ed_new_username.getText().toString();
                n_password = ed_new_password.getText().toString();
                c_password = ed_confirm_password.getText().toString();

                if(e_username.equals(blank))
                {
                    Toast.makeText(getApplicationContext(), "Please enter Employee Username", Toast.LENGTH_SHORT).show();
                }
                else if(e_password.equals(blank) || e_password.length() <6)
                {
                    ed_employee_password.setError("You must have 6 Character’s in Password");
                    return;
                }
                else if(n_username.equals(blank))
                {
                    Toast.makeText(getApplicationContext(), "Please enter New Username", Toast.LENGTH_SHORT).show();
                }
                else if(n_password.equals(blank) || n_password.length() <6)
                {
                    ed_new_password.setError("You must have 6 Character’s in Password");
                    return;
                }
                else if(c_password.equals(blank) || c_password.length() <6)
                {
                    ed_confirm_password.setError("You must have 6 Character’s in Password");
                    return;
                }
                else if(n_password.equals(c_password))
                {
                    TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                    IMEI = tm.getDeviceId();//imei no is captured
                    send();
                    receive();
                    if(username.equals(e_username))
                    {
                        database.signup_detail(n_username, n_password, IMEI);
                        Toast.makeText(getApplicationContext(), " New User Registered Successfully", Toast.LENGTH_SHORT).show();
                        i = new Intent(getApplicationContext(),Login.class);
                        startActivity(i);
                        finish();
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "User is already registered with another Mobile Device", Toast.LENGTH_SHORT).show();                    
                    }
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "New Password and Confirm Password does not match", Toast.LENGTH_SHORT).show();
                }

            break;
            case R.id.btn_cancle_signup:
                i = new Intent(getApplicationContext(),Login.class);
                startActivity(i);
                finish();
            break;
        }
    }

    public void send()
    {
        Toast.makeText(getApplicationContext(), "in send...", Toast.LENGTH_SHORT).show();
        start = "@STR";
        code =  "01";
        end = "000$";
        //Padding logic 
        employee_username_padd = e_username;
        employee_password_padd = e_password;
        int fixlen= 10;
        int eidlen= e_username.length();
        int epasslen= e_password.length();

        if(fixlen>eidlen)
        {
            for(int i=0;i<(fixlen-eidlen);i++)
            {
                employee_username_padd="*"+employee_username_padd;
            }
        }
        if(fixlen>epasslen)
        {
            for(int i=0;i<(fixlen-epasslen);i++)
            {
                employee_password_padd="*"+employee_password_padd;
            }
        }

        data = start+code+employee_username_padd+employee_password_padd+IMEI+end;
        try {
            client=new Socket(ServerIP,2222);
            printwriter=new PrintWriter(client.getOutputStream(),true);
            printwriter.write(data);
            printwriter.flush();
            printwriter.close();
            //receive();
            client.close();

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public String receive()
    {
        try {
            //Toast.makeText(getApplicationContext(), "in receive", Toast.LENGTH_SHORT).show();
            serverSocket = new ServerSocket(1111);
            clientSocket = serverSocket.accept();
            inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
            bufferedReader = new BufferedReader(inputStreamReader); //get the client message
            msg = bufferedReader.readLine();
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            String start1= msg.substring(0,4);
            String code1= msg.substring(4, 6);
            String result1= msg.substring(6, 16);
            String end1= msg.substring(16, 20);

            if(start1.equals("@STR") && code1.equals("01") && end1.equals("999$"))
            {
                /*Toast.makeText(getApplicationContext(), start1, Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), code1, Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), result1, Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), end1, Toast.LENGTH_SHORT).show();*/

                result = result1;
                remove_padding();
            }else
            {
                /*Toast.makeText(getApplicationContext(), "in else"+start1, Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "in else"+code1, Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "in else"+result1, Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "in else"+end1, Toast.LENGTH_SHORT).show();*/
                result = result1;
                remove_padding();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
    public String remove_padding()
    {
        //String starusername=usernamepasswordIMEI.substring(0,10);

        char star='*';

                int count=0;

                    if(result.charAt(0) == star)
                    {
                        for(int i=0;i<result.length();i++)
                        {

                            if(result.charAt(i) == star)
                            {
                                count++;
                            }
                            /*break;*/
                        }//end for
                        username=result.substring(count,result.length());
                    }
                    else
                    {
                        System.out.println("user name String doesn't start with *");
                        username=result;
                    }


                    return username;

    }

After Run it shows below error :

07-18 15:13:09.502: E/AndroidRuntime(32249): FATAL EXCEPTION: main
07-18 15:13:09.502: E/AndroidRuntime(32249): android.os.NetworkOnMainThreadException
07-18 15:13:09.502: E/AndroidRuntime(32249):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at libcore.io.IoBridge.connect(IoBridge.java:112)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at java.net.Socket.startupSocket(Socket.java:566)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at java.net.Socket.tryAllAddresses(Socket.java:127)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at java.net.Socket.<init>(Socket.java:177)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at java.net.Socket.<init>(Socket.java:149)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at com.ami.wbs.Signup.send(Signup.java:161)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at com.ami.wbs.Signup.onClick(Signup.java:102)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at android.view.View.performClick(View.java:4117)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at android.view.View$PerformClick.run(View.java:17041)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at android.os.Handler.handleCallback(Handler.java:615)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at android.os.Looper.loop(Looper.java:137)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at android.app.ActivityThread.main(ActivityThread.java:4747)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at java.lang.reflect.Method.invokeNative(Native Method)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at java.lang.reflect.Method.invoke(Method.java:511)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-18 15:13:09.502: E/AndroidRuntime(32249)`enter code here`:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-18 15:13:09.502: E/AndroidRuntime(32249):    at dalvik.system.NativeStart.main(Native Method)
  • you should use a thread for network related operation. use thread or asynctask. and search on so will find lots of similar posts – Raghunandan Jul 18 '13 at 10:02

2 Answers2

0

you can not use network operations from main UI thread. To do this you have to use those in different threads ( better use AsyncTask)

For more info check doc here. From the doc

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document Designing for Responsiveness.

Also see StrictMode.

stinepike
  • 54,068
  • 14
  • 92
  • 112
0
  1. (Best Way) As it's name indicates, exception is in main thread, so there should be use a separate thread or use asynctask.
  2. (Temporary solution) Add this code in your activity

     ThreadPolicy tp = ThreadPolicy.LAX;
     StrictMode.setThreadPolicy(tp);
    
  3. As it's a problem in Gingerbread(API). You can also change your targetSDK to 9. But this is not a good solution.

Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59