使用Android应用程序登录drupal站点

时间:2014-08-22 13:29:52

标签: java android json drupal login

我在drupal中启用了服务模块,并使用json启用了服务模块中的登录使用。每次我尝试登录时都会收到致命错误,应用程序已关闭。 这是代码。问题是我也不知道为什么代码试图执行catch。我已正确输入用户名和密码。请任何帮助都会很棒。

 /**
 * is called when the user presses the login button
 *
 * @param view
 */
public void login(View view) {
    user_name = (EditText) findViewById(R.id.User_name);
    password = (EditText) findViewById(R.id.Password);

    //get the username and password entered by the user
    String username = user_name.getText().toString();
    String pass = password.getText().toString();
    EditText myEditText = (EditText) findViewById(R.id.Password);
    EditText Text = (EditText) findViewById(R.id.User_name);

    //hide the keyboard from one layout to another
    InputMethodManager hide_keyboard = (InputMethodManager) getSystemService(
            Context.INPUT_METHOD_SERVICE);
    hide_keyboard.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
    InputMethodManager hide = (InputMethodManager) getSystemService(
            Context.INPUT_METHOD_SERVICE);
    hide.hideSoftInputFromWindow(Text.getWindowToken(), 0);

    //shows an alert box if the one of the texts are empty
    if (username.matches("") || pass.matches("")) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Missing Information");
        alertDialog.setMessage("Please fill in the required fields");
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //ok
            }
        });
        alertDialog.show();
    } else {
        Cursor mCursor = database.rawQuery("SELECT * FROM  mytable", null);

        if (mCursor.moveToFirst()) {
            // update the database if it is not empty
            ContentValues values = new ContentValues();
            long id = 0;
            values.put("name", username);
            values.put("password", pass);
            database.update("mytable", values, "id=" + id, null);

        } else {
            // Database is empty - save the first username and password
            database.execSQL("insert into mytable values('" + 0 + "', '" + username + "','" + pass + "')");

        }
        LoginProcess login = new LoginProcess();
        if (login.doInBackground() == 0) {
            setContentView(R.layout.success);
        } else {


            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Error Logging in");
            alertDialog.setMessage("Please make sure the username and password are correct");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //ok
                }
            });
            alertDialog.show();
        }
    }
    }





 private class LoginProcess extends AsyncTask<String, Integer, Integer> {
    protected Integer doInBackground(String... params) {

        HttpClient httpclient = new DefaultHttpClient();

        //set the remote endpoint URL
        HttpPost httppost = new HttpPost("http://192.168.1.65/drupal/androidrpc/user/login");


        try {
            //get the UI elements for username and password
            EditText username = (EditText) findViewById(R.id.User_name);
            EditText password = (EditText) findViewById(R.id.Password);

            JSONObject json = new JSONObject();
            //extract the username and password from UI elements and create a JSON object
            json.put("username", username.getText().toString().trim());
            json.put("password", password.getText().toString().trim());

            //add serialised JSON object into POST request
            StringEntity se = new StringEntity(json.toString());
            //set request content type
            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            httppost.setEntity(se);

            //send the POST request
            HttpResponse response = httpclient.execute(httppost);

            //read the response from Services endpoint
            String jsonResponse = EntityUtils.toString(response.getEntity());

            JSONObject jsonObject = new JSONObject(jsonResponse);
            //read the session information
            session_name = jsonObject.getString("session_name");
            session_id = jsonObject.getString("sessid");


        }
        catch (Exception e) {
           Log.v("Error Logging in:",e.getMessage());//here is the error

        }
    return 0;
    }

0 个答案:

没有答案