2016-08-17 94 views
0

LoginActivity.java是我打开android应用程序后运行的第一件事情,并且登录屏幕显示要求从数据库中检索到的电话号码和密码。我有一个与我的电脑上运行的这个项目相关的mysql数据库,但是如何从数据库中的应用程序检索phoneno的位置完全找到它。和密码从?这个LoginActivity在哪里检索phoneno。和密码从?

LoginActivity.java

package com.example.ankit.mrestro.Controller; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 

import com.baidu.android.pushservice.PushConstants; 
import com.baidu.android.pushservice.PushManager; 
import com.squareup.otto.Subscribe; 
import com.example.ankit.mrestro.Bus.BusProvider; 
import com.example.ankit.mrestro.Bus.LoginEvent; 
import com.example.ankit.mrestro.Bus.LoginSuccessEvent; 
import com.example.ankit.mrestro.Bus.PushRegisterEvent; 
import com.example.ankit.mrestro.R; 
import com.example.ankit.mrestro.model.LoginResult; 
import com.example.ankit.mrestro.services.DataService; 
import com.example.ankit.mrestro.services.RESTrepository; 

public class LoginActivity extends Activity { 
    public static final String PREF_ACCOUNT_ID = "cust_id"; 
    public static final String PREF_TOKEN = "accessToken"; 
    public static final String SHARED_PREF_DB_NAME = "loginResult"; 
    private ProgressDialog progressDialog; 

    public static Intent createIntent(Context c) { 
     return new Intent(c, LoginActivity.class); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     DataService.init(); 
     progressDialog = new ProgressDialog(this); 

     /** 
     * Check either we are already logged in 
     */ 
     SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF_DB_NAME, 0); 
     if (sharedPreferences.getString(PREF_TOKEN, "").length() != 0) { 
      RESTrepository.setToken(sharedPreferences.getString(PREF_TOKEN, "")); 
      RESTrepository.setUser_id(sharedPreferences.getInt(PREF_ACCOUNT_ID, 0)); 
      goToMainActivity(); 
     } 

     setContentView(R.layout.activity_login); 
     PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, 
       "hwfeocSIPlgKTasIuARPREnS"); 
     //SharedPreferences preferences=getSharedPreferences("pushService",0); 
     //String userId=preferences.getString("user_id","no data"); 
     //Toast.makeText(this,"user id is:"+userId,Toast.LENGTH_SHORT).show(); 
     Button loginButton=(Button)findViewById(R.id.email_sign_in_button); 
     loginButton.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View v){ 
       String phoneno=((TextView)findViewById(R.id.email)).getText().toString(); 
       String password=((TextView)findViewById(R.id.password)).getText().toString(); 

       //  Toast.makeText(getBaseContext(),"login..."+phoneno+"..."+password,Toast.LENGTH_SHORT).show(); 
       progressDialog.show(); 
       BusProvider.get().post(new LoginEvent(phoneno,password)); 
      } 
     }); 
    } 

    @Override 
    protected void onResume(){ 
     super.onResume(); 
     BusProvider.get().register(this); 
    } 

    @Override 
    protected void onPause(){ 
     super.onPause(); 
     BusProvider.get().unregister(this); 
    } 

    @Subscribe 
    public void onLoginSuccessEvent(LoginSuccessEvent loginSuccessEvent){ 
     progressDialog.hide(); 
     LoginResult result=loginSuccessEvent.getResult(); 
     if (result != null) { 
      // Toast.makeText(this,result.getCust_id()+result.getCust_name()+result.getCust_access_token(),Toast.LENGTH_SHORT).show(); 
      //Toast.makeText(this,"Login Success",Toast.LENGTH_SHORT).show(); 
      SharedPreferences preferences = this.getSharedPreferences(SHARED_PREF_DB_NAME, MODE_PRIVATE); 
      preferences.edit().putString(PREF_TOKEN,result.getCust_access_token()).commit(); 
      preferences.edit().putInt(PREF_ACCOUNT_ID,result.getCust_id()).commit(); 
      SharedPreferences pushPreferences=this.getSharedPreferences("pushService",0); 
      BusProvider.get().post(new PushRegisterEvent 
        (result.getCust_id(),result.getCust_access_token(),pushPreferences.getString("user_id",""))); 
      goToMainActivity(); 

     } else { 
      Toast.makeText(this, "Unable to login, please retry", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    private void goToMainActivity() { 
     Intent intent = new Intent(this, MainActivity.class); 
     startActivity(intent); 
     finish(); 
    } 
} 

回答

0

在登录活动的按钮单击您所得到的电话号码,你把它发送到服务器和密码onLoginSuccessEvent你得到你的回应,并把它保存到sharePreferences

+0

但它一直说“无法登录,请再试一次!”我似乎无法登录到应用程序。我不在这里? – Bik

+0

你在sql中存储了你的电话号码和密码,换句话说你注册了吗? –

+0

基本上在服务器上您的用户名和密码将与您发送的用户名和密码进行比较,将其放入editText –