如何避免在Android中强制关闭应用程序

时间:2016-10-09 03:42:47

标签: android exception mobile

我试过在互联网上搜索解决方案但遗憾的是我无法找到答案。我尝试使用try catch来捕获错误异常,但它仍然无法正常工作。 这是我的代码。我有私有类LoginTask

private class LoginTask extends AsyncTask<String,String,JSONObject> {


    private String[] privateCredentials;
    private String privateRequest;
    private String errorMessage = "";
    //initialize all here
    //constructor
    LoginTask(String[] credentials,String request) {
        this.privateRequest = request;
        this.privateCredentials = credentials;


    }

    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);

        if(this.privateRequest=="login"){
            try {
                String response = result.getString("status");

                if(response.equals("ok")){
                    onLoginSuccess(result.getString("username"),result.getString("full_name"),result.getInt("user_id"));

                }else{
                    onLoginFails();
                }

            } catch (JSONException e) {
                if(errorMessage!=""){
                    Toast ts;
                    ts = Toast.makeText(LoginActivity.this,errorMessage,Toast.LENGTH_LONG);
                    ts.show();
                }
                //e.printStackTrace();
            }
        }


    }




    @Override
    protected JSONObject doInBackground(String... params) {

        String result = "";
        JSONObject resultObj = null;
        HttpURLConnection con = null;
        BufferedReader br = null;
        JSONObject cred = new JSONObject();

        if(this.privateRequest=="login"){

            try {

                cred.put("username", this.privateCredentials[0]);
                cred.put("password", this.privateCredentials[1]);

                URL url = new URL(params[0]);


                con = (HttpURLConnection) url.openConnection();
                ;
                con.setDoOutput(true);
                con.setDoInput(true);
                con.setRequestProperty("Content-Type", "application/json");
                con.setRequestProperty("Accept", "application/json");
                con.setRequestMethod("POST");
                con.connect();
                OutputStream outputStream = con.getOutputStream();
                outputStream.write(cred.toString().getBytes());
                InputStream stream = con.getInputStream();
                br = new BufferedReader(new InputStreamReader(stream));
                StringBuffer buffer = new StringBuffer();
                String line = "";
                while ((line = br.readLine()) != null) {
                    buffer.append(line);
                }

                //get the result
                JSONObject jsonObj = new JSONObject(buffer.toString());
                resultObj = jsonObj;

                // return buffer.toString();


            }catch (JSONException e) {
                errorMessage = e.getMessage();
                final String error = e.getMessage();

                //e.printStackTrace();
                runOnUiThread(new Runnable(){
                    public void run() {
                        //ErrorDialog(e.getMessage());
                        Toast ts;
                        ts = Toast.makeText(LoginActivity.this,error,Toast.LENGTH_LONG);
                        ts.show();
                    }
                });
            } catch (ProtocolException e) {
               errorMessage = e.getMessage();
                final String error = e.getMessage();

                //e.printStackTrace();
                runOnUiThread(new Runnable(){
                    public void run() {
                        //ErrorDialog(e.getMessage());
                        Toast ts;
                        ts = Toast.makeText(LoginActivity.this,error,Toast.LENGTH_LONG);
                        ts.show();
                    }
                });

                //e.printStackTrace();

            } catch (IOException e) {
               errorMessage = e.getMessage();
                final String error = e.getMessage();

                //e.printStackTrace();
                runOnUiThread(new Runnable(){
                    public void run() {
                        //ErrorDialog(e.getMessage());
                        Toast ts;
                        ts = Toast.makeText(LoginActivity.this,error,Toast.LENGTH_LONG);
                        ts.show();
                    }
                });

                //e.printStackTrace();

            } finally {

                if(con!=null) {
                    con.disconnect();
                }

            }

            return resultObj;
        }

        return null;
    }
}

这是登录活动中的事件监听器代码。

//when clicking the login button
    loginBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            //do now the login process
            userText.setVisibility(view.INVISIBLE);
            passwordText.setVisibility(view.INVISIBLE);
            tvRegister.setVisibility(view.INVISIBLE);
            umlogo.setVisibility(view.INVISIBLE);


            //set textviews to invisible
           /* tv[0].setVisibility(view.INVISIBLE);
            tv[1].setVisibility(view.INVISIBLE);*/

            //set also the button to invisible
            loginBtn.setVisibility(view.INVISIBLE);


            //set visible the progress bar
            pb.setVisibility(view.VISIBLE);

            //set now the user login credentials
            credentials[0] = userText.getText().toString();
            credentials[1] = passwordText.getText().toString();
            loginTask = new LoginTask(credentials,"login");
            //loginTask.execute("http://10.0.2.2/sampleRequest.php");
            //loginTask.execute("http://10.0.2.2/motorpool_june_2016_laravel/public/mobile/login");


            loginTask.execute("http://128.199.105.49/mobile/login");

            //SessionHolder.login(credentials, la);

        }
    });

然而它仍然无效。请帮忙。 :(

1 个答案:

答案 0 :(得分:0)

您无法在java中将void *GetMember() { return m_data; } return进行比较。你必须写如下:

m_data

String测试引用相等性(它们是否是同一个对象)