2015-04-07 60 views
0

嘿傢伙我正在處理一個項目,它處理用戶輸入用戶名和密碼,然後他們應該登錄到他們的電子郵件accounts.i've搜索網絡,但我的問題是但我想我知道如何登錄用戶,但我不知道該怎麼從那裏通過httpclient和httppost登錄到帳戶

做這裏去我的代碼部分:

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 

問題是現在我應該怎麼知道服務器已登錄用戶,然後如何使用它來顯示另一個用戶的活動,他可以看到他自己的郵件。謝謝你,我真的很需要:)))

回答

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

相關問題