尝试连接互联网时,应用崩溃

时间:2016-02-28 11:01:19

标签: java android

我正在使用android studio,每当我尝试连接到互联网时,它会显示Dialog“不幸'应用名称已停止”,然后崩溃。 我已经更新了清单文件以获得许可。请提供任何帮助,这可能会有所帮助。 这是代码:

public class Login extends Activity {
    TextView msg;
    String user,pass;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        final EditText password;
        TextView regLabel;
        Button loginbt;
        final EditText username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        regLabel = (TextView) findViewById(R.id.register_label);
        msg = (TextView) findViewById(R.id.alert);
        regLabel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent register_form = new Intent(Login.this,Register.class);
                startActivity(register_form);
            }
        });
        loginbt = (Button) findViewById(R.id.login);
        loginbt.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                user = username.getText().toString();
                pass = password.getText().toString();
                if(user.length()>0 && pass.length()>0) {
                    try {
                        new LoginProcess().execute("http://url.com");
                    } catch (Exception le) {
                        msg.setText("Error:" + le);
                    }
                }else{
                    msg.setText("Please Enter Username and Password!");
                }
            }
        });
    }

    public class LoginProcess extends AsyncTask<String, Void, Void> {
        private final HttpClient Client = new DefaultHttpClient();
        private String Content;
        private String Error = null;
        private ProgressDialog Dialog = new ProgressDialog(Login.this);

        protected void onPreExecute(){
            Dialog.setMessage("Checking Authentication..");
            Dialog.show();
        }
        // Call after onPreExecute method
        protected Void doInBackground(String... urls) {
            try {
                HttpGet httpget = new HttpGet(urls[0]);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                Content = Client.execute(httpget, responseHandler);
            } catch (ClientProtocolException e) {
                Error = e.getMessage();
                cancel(true);
            } catch (IOException e) {
                Error = e.getMessage();
                cancel(true);
            }

            return null;
        }

        protected void onPostExecute(Void unused) {
            if (Error != null) {
                msg.setText("Error in Login: " + Error);
            } else {
                try {
                    JSONObject jsonObj = new JSONObject(Content);
                    String orgPass = jsonObj.getString("password");
                    if (orgPass.equals(pass)) {
                        Intent rp = new Intent(Login.this, Menu.class);
                        startActivity(rp);
                        finish();
                    } else {
                        msg.setText("Wrong Password");
                    }
                } catch (Exception je) {
                    msg.setText("Error:" + je);
                }
            }
        }

    }
}

0 个答案:

没有答案