-1

我的 android 登录功能出现问题,我正在使用 android 设备 4.1....当我运行我的应用程序时,它会立即终止并控制台显示错误:

以下是我的代码:请指教

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;

    }

运行后显示以下错误:

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)
4

2 回答 2

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.

于 2013-07-18T10:15:49.540 回答
0

您不能从主 UI 线程使用网络操作。为此,您必须在不同的线程中使用它们(最好使用AsyncTask

有关更多信息,请在此处查看文档。从文档

当应用程序尝试在其主线程上执行网络操作时引发的异常。

这仅针对面向 Honeycomb SDK 或更高版本的应用程序抛出。允许以早期 SDK 版本为目标的应用程序在其主事件循环线程上进行网络连接,但非常不鼓励这样做。请参阅文档设计响应性。

另请参阅严格模式。

于 2013-07-18T10:04:39.830 回答