0

I am using the following to make a call to a PHP script to log a user in:

public class MainActivity extends Activity {

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

        Button loginButton = (Button) findViewById(R.id.loginButton);

        loginButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new loginClass().execute();
            }
        });
    }

    class loginClass extends AsyncTask<String, String, Void> {
        private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        InputStream is = null ;
        String result = "";
        protected void onPreExecute() {
            progressDialog.setMessage("Logging you in...");
            progressDialog.show();
            progressDialog.setCancelable(false);
            progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface arg0) {
                    loginClass.this.cancel(true);
                }
            });
        }
        @Override
        protected Void doInBackground(String... params) {
            try {
                HttpPost httpPost = new HttpPost("http://www.example.com/login.php");
                HttpParams httpParameters = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
                HttpConnectionParams.setSoTimeout(httpParameters, 3000);
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                EditText uname = (EditText)findViewById(R.id.usernameField);
                String username = uname.getText().toString();
                EditText pword = (EditText)findViewById(R.id.passwordField);
                String password = pword.getText().toString();
                nameValuePairs.add(new BasicNameValuePair("username", username));
                nameValuePairs.add(new BasicNameValuePair("password", password));
                HttpClient httpClient = new DefaultHttpClient(httpParameters);
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is =  httpEntity.getContent();
                Log.e("log_tag", "Open Connection");


            } catch(ConnectTimeoutException e){
                Log.e("Timeout Exception: ", e.toString());
                result = null;
            } catch(SocketTimeoutException ste){
                Log.e("Timeout Exception: ", ste.toString());
                result = null;
            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection "+e.toString());
                result = null;
            }

            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                String line = "";
                while((line=br.readLine())!=null){
                    sb.append(line+"\n");
                }
                is.close();
                result=sb.toString();
                Log.e("log_tag", "Result: "+result);
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result "+e.toString());
                result = null;
            }
            return null;

        }

        protected void onPostExecute(Void v) {
            Log.e("log_tag", "Execute Started - Result: "+result);
            if (result == null){
                this.progressDialog.dismiss();
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                alertDialog.setTitle("Connection Error");
                alertDialog.setMessage("There was a problem with your connection.");
                alertDialog.setButton("Exit", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                alertDialog.setButton2("Try Again", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        new loginClass().execute();

                    }
                });
                alertDialog.show();
            }else if("0".equals(result.toString())){
                this.progressDialog.dismiss();
                Toast.makeText(MainActivity.this, "Invalid Username/Password.  Please try again.", Toast.LENGTH_SHORT).show();
            }else if("1".equals(result.toString())){
                this.progressDialog.dismiss();
                Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
            }else{
                this.progressDialog.dismiss();
                Toast.makeText(MainActivity.this, "Something Else Happened", Toast.LENGTH_SHORT).show();
            }

        }
    }


}

In LogCat I am showing the Execute Started - Result: 0, however it is not catching the result in the if statement, so the the last else statement is firing. Is "0".equals(result.toString()) the proper way to handle the results?

4

1 回答 1

1

使用String.contains()方法。

尝试一下result.toString().contains("0")result.toString().contains("1") 这应该可以。

我建议将响应作为 JSON 对象而不是普通的 HTTP 响应来检索。

于 2013-07-31T16:58:06.640 回答