通过httpclient和httppost登录帐户

时间:2015-04-07 11:58:03

标签: java android login httpclient

嘿,伙计们我正在处理一个项目,该项目涉及用户输入用户名和密码,然后他们应该登录他们的电子邮件帐户。我已经搜索了网但我的问题是,虽然我认为我知道如何登录用户,但我不知道该怎么做

这是我的代码的一部分:

private class AsyncTaskOperation extends AsyncTask <String, Void, Void>
{

     private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);
     String ciao="";
    protected void onPreExecute() {
        // Display the loading spinner
        Dialog.setMessage("Loading... Please wait.. ");
        Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        Dialog.setInverseBackgroundForced(false);
        Dialog.setCancelable(false);


        Dialog.show();
    }

    @Override
    protected Void doInBackground(String... paramsObj) {
       HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(
            "https://www.elenoon.ir/mail/");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("user", user.getText()
            .toString()));
    nameValuePairs.add(new BasicNameValuePair("pass", pass.getText()
            .toString()));

    try {

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse execute = httpclient.execute(httppost);
        InputStream content;
        content = execute.getEntity().getContent();
        BufferedReader buffer = new BufferedReader(new InputStreamReader(
                content));
        String s = "";
        String test = "";
        while ((s = buffer.readLine()) != null) {
            test += s;
        }
        if (test.contains("U")) {
            // reset field and toast
            Toast.makeText(getBaseContext(), "Login Failed",     Toast.LENGTH_SHORT)
                    .show();
        } else {
            // Intent intent = new Intent(MainActivity.this,Form.class);
            // this.startActivity(intent);
            Toast.makeText(getBaseContext(), "Login Successful",
                    Toast.LENGTH_SHORT).show();
        }
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        return null;
    }
    protected void onPostExecute(Void unused) 
    {
        // Close progress dialog
        Dialog.dismiss();
        // Do actions after end of the HTTPGet or POST Method

    } // End of method onPostExecute

现在的问题是我应该怎么知道服务器已登录用户,然后如何使用它来显示用户可以看到自己的邮件的另一个活动。谢谢你真的我真的需要:))))

1 个答案:

答案 0 :(得分:0)

要在服务器上登录用户,您可以尝试这样的操作。包装getCurrentUser() API方法以检测它是否看到空用户并在此情况下强制登录。

doGet method

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  UserService userService=UserServiceFactory.getUserService();
  User user=userService.getCurrentUser();
  if (user != null) {
    resp.setContentType("text/plain");
    resp.getWriter().println("Hello, " + user.getNickname());
  }else {
    //Redirecting to createLoginURL method will force the user to login.
    resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
  }
}

要使登录成功并进入主要活动,请onPostExecute

@Override
protected void onPostExecute(final Boolean success) {
    mAuthTask = null;
    showProgress(false);
    if (success) {
        finish();
        Intent myIntent = new Intent(LoginActivity.this,MyMainActivity.class);
        LoginActivity.this.startActivity(myIntent);
    } else {
        mPasswordView.setError(getString(R.string.error_incorrect_password));
        mPasswordView.requestFocus();
    }
}

有关详情,请点击此处:Login Activity Template generated activity