Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android应用程序中保存登录凭据_Android_Codeigniter - Fatal编程技术网

在android应用程序中保存登录凭据

在android应用程序中保存登录凭据,android,codeigniter,Android,Codeigniter,我正在开发一个应用程序,用户可以在其中评价图片。要验证用户身份,我想使用他/她的gmail帐户。如果用户愿意,可以随时更改其评级。我正在使用codeigniter中的RESTAPI在服务器上保存评级。如何做到这一点?如果用户再次登录,我如何记得他?我应该如何设计相同的数据库 这就是我登录时要做的 import java.io.InputStream; import android.app.Activity; import android.content.Intent; import andro

我正在开发一个应用程序,用户可以在其中评价图片。要验证用户身份,我想使用他/她的gmail帐户。如果用户愿意,可以随时更改其评级。我正在使用codeigniter中的RESTAPI在服务器上保存评级。如何做到这一点?如果用户再次登录,我如何记得他?我应该如何设计相同的数据库

这就是我登录时要做的

import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.plus.Plus;
import com.google.android.gms.plus.model.people.Person;

public class MainActivity extends Activity implements OnClickListener,
        ConnectionCallbacks, OnConnectionFailedListener {

    private static final int RC_SIGN_IN = 0;
    // Logcat tag
    private static final String TAG = "MainActivity";

    // Profile pic image size in pixels
    private static final int PROFILE_PIC_SIZE = 400;

    // Google client to interact with Google API
    private GoogleApiClient mGoogleApiClient;

    /**
     * A flag indicating that a PendingIntent is in progress and prevents us
     * from starting further intents.
     */
    private boolean mIntentInProgress;

    private boolean mSignInClicked;

    private ConnectionResult mConnectionResult;

    private SignInButton btnSignIn;
    private Button btnSignOut, btnRevokeAccess;
    private ImageView imgProfilePic;
    private TextView txtName, txtEmail;
    private LinearLayout llProfileLayout;

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

        btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in);
        btnSignOut = (Button) findViewById(R.id.btn_sign_out);
        btnRevokeAccess = (Button) findViewById(R.id.btn_revoke_access);
        imgProfilePic = (ImageView) findViewById(R.id.imgProfilePic);
        txtName = (TextView) findViewById(R.id.txtName);
        txtEmail = (TextView) findViewById(R.id.txtEmail);
        llProfileLayout = (LinearLayout) findViewById(R.id.llProfile);

        // Button click listeners
        btnSignIn.setOnClickListener(this);
        btnSignOut.setOnClickListener(this);
        btnRevokeAccess.setOnClickListener(this);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_LOGIN).build();
    }

    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    /**
     * Method to resolve any signin errors
     * */
    private void resolveSignInError() {
        if (mConnectionResult.hasResolution()) {
            try {
                mIntentInProgress = true;
                mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
            } catch (SendIntentException e) {
                mIntentInProgress = false;
                mGoogleApiClient.connect();
            }
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        if (!result.hasResolution()) {
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                    0).show();
            return;
        }

        if (!mIntentInProgress) {
            // Store the ConnectionResult for later usage
            mConnectionResult = result;

            if (mSignInClicked) {
                // The user has already clicked 'sign-in' so we attempt to
                // resolve all
                // errors until the user is signed in, or they cancel.
                resolveSignInError();
            }
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int responseCode,
                                    Intent intent) {
        if (requestCode == RC_SIGN_IN) {
            if (responseCode != RESULT_OK) {
                mSignInClicked = false;
            }

            mIntentInProgress = false;

            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect();
            }
        }
    }

    @Override
    public void onConnected(Bundle arg0) {
        mSignInClicked = false;
        Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();

        // Get user's information
        getProfileInformation();

        // Update the UI after signin
        updateUI(true);

    }

    /**
     * Updating the UI, showing/hiding buttons and profile layout
     * */
    private void updateUI(boolean isSignedIn) {
        if (isSignedIn) {
            btnSignIn.setVisibility(View.GONE);
            btnSignOut.setVisibility(View.VISIBLE);
            btnRevokeAccess.setVisibility(View.VISIBLE);
            llProfileLayout.setVisibility(View.VISIBLE);
        } else {
            btnSignIn.setVisibility(View.VISIBLE);
            btnSignOut.setVisibility(View.GONE);
            btnRevokeAccess.setVisibility(View.GONE);
            llProfileLayout.setVisibility(View.GONE);
        }
    }

    /**
     * Fetching user's information name, email, profile pic
     * */
    private void getProfileInformation() {
        try {
            if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                Person currentPerson = Plus.PeopleApi
                        .getCurrentPerson(mGoogleApiClient);
                String personName = currentPerson.getDisplayName();
                String personPhotoUrl = currentPerson.getImage().getUrl();
                String personGooglePlusProfile = currentPerson.getUrl();
                String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

                Log.e(TAG, "Name: " + personName + ", plusProfile: "
                        + personGooglePlusProfile + ", email: " + email
                        + ", Image: " + personPhotoUrl);

                txtName.setText(personName);
                txtEmail.setText(email);

                // by default the profile url gives 50x50 px image only
                // we can replace the value with whatever dimension we want by
                // replacing sz=X
                personPhotoUrl = personPhotoUrl.substring(0,
                        personPhotoUrl.length() - 2)
                        + PROFILE_PIC_SIZE;

                new LoadProfileImage(imgProfilePic).execute(personPhotoUrl);

            } else {
                Toast.makeText(getApplicationContext(),
                        "Person information is null", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onConnectionSuspended(int arg0) {
        mGoogleApiClient.connect();
        updateUI(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    /**
     * Button on click listener
     * */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sign_in:
                // Signin button clicked
                signInWithGplus();
                break;
            case R.id.btn_sign_out:
                // Signout button clicked
                signOutFromGplus();
                break;
            case R.id.btn_revoke_access:
                // Revoke access button clicked
                revokeGplusAccess();
                break;
        }
    }

    /**
     * Sign-in into google
     * */
    private void signInWithGplus() {
        if (!mGoogleApiClient.isConnecting()) {
            mSignInClicked = true;
            resolveSignInError();
        }
    }

    /**
     * Sign-out from google
     * */
    private void signOutFromGplus() {
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            mGoogleApiClient.connect();
            updateUI(false);
        }
    }

    /**
     * Revoking access from google
     * */
    private void revokeGplusAccess() {
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
                    .setResultCallback(new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status arg0) {
                            Log.e(TAG, "User access revoked!");
                            mGoogleApiClient.connect();
                            updateUI(false);
                        }

                    });
        }
    }

    /**
     * Background Async task to load user profile picture from url
     * */
    private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public LoadProfileImage(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

}
import java.io.InputStream;
导入android.app.Activity;
导入android.content.Intent;
导入android.content.IntentSender.SendIntentException;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.GooglePlayServicesUtil;
导入com.google.android.gms.common.SignInButton;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.google.android.gms.common.api.GoogleAppClient.ConnectionCallbacks;
导入com.google.android.gms.common.api.GoogleAppClient.OnConnectionFailedListener;
导入com.google.android.gms.common.api.ResultCallback;
导入com.google.android.gms.common.api.Status;
导入com.google.android.gms.plus.plus;
导入com.google.android.gms.plus.model.people.Person;
公共类MainActivity扩展活动实现OnClickListener,
ConnectionCallbacks,OnConnectionFailedListener{
私有静态最终int RC_SIGN_IN=0;
//Logcat标签
私有静态最终字符串TAG=“MainActivity”;
//配置文件pic图像大小(像素)
私有静态最终整数配置文件图片大小=400;
//Google客户端与Google API交互
私人GoogleapClient MGoogleapClient;
/**
*一种标志,表明悬挂式帐篷正在进行中并阻止我们
*从开始进一步的意图。
*/
私有布尔mIntentInProgress;
私有布尔msignincled;
私有连接结果mConnectionResult;
私人签名按钮;
专用按钮btnSignOut、btnRevokeAccess;
私有ImageView imgProfilePic;
私有文本视图txtName,txtmail;
专用线路布局图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSignIn=(签名按钮)findViewById(R.id.btn\u sign\u in);
btnSignOut=(按钮)findViewById(R.id.btn\u注销);
btnRevokeAccess=(按钮)findViewById(R.id.btn\u revoke\u访问);
imgProfilePic=(ImageView)findViewById(R.id.imgProfilePic);
txtName=(TextView)findViewById(R.id.txtName);
txtEmail=(TextView)findViewById(R.id.txtEmail);
llProfileLayout=(LinearLayout)findviewbyd(R.id.llProfile);
//按钮单击侦听器
btnSignIn.setOnClickListener(此);
btnSignOut.setOnClickListener(这个);
btnRevokeAccess.setOnClickListener(此);
mgoogleapclient=新的Googleapclient.Builder(此)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE\u Plus\u LOGIN).build();
}
受保护的void onStart(){
super.onStart();
mGoogleApiClient.connect();
}
受保护的void onStop(){
super.onStop();
if(mgoogleapClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
/**
*方法来解决任何登录错误
* */
私有无效解析错误(){
if(mcConnectionResult.hasResolution()){
试一试{
mIntentInProgress=true;
mConnectionResult.StartResult解决方案(这是RC\u登录);
}捕获(发送){
mIntentInProgress=false;
mGoogleApiClient.connect();
}
}
}
@凌驾
连接失败的公共void(连接结果){
如果(!result.hasResolution()){
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),此,
0)show();
返回;
}
如果(!mIntentInProgress){
//存储ConnectionResult供以后使用
mConnectionResult=结果;
如果(mSignInClicked){
//用户已单击“登录”,因此我们尝试
//解决所有问题
//错误,直到用户登录或取消。
ResolveSignError();
}
}
}
@凌驾
ActivityResult上的受保护无效(int请求代码、int响应代码、,
意图(意图){
if(requestCode==RC\u登录){
if(responseCode!=结果\u正常){
mSignInClicked=false;
}
mIntentInProgress=false;
如果(!mgoogleapClient.isConnecting()){
mGoogleApiClient.connect();
}
}
}
@凌驾
未连接的公共空间(捆绑arg0){
mSignInClicked=false;
Toast.makeText(此“用户已连接!”,Toast.LENGTH_LONG.show();
//获取用户信息
getProfileInformation();
//登录后更新UI
更新(真);
}
/**
*更新UI、显示/隐藏按钮和配置文件布局
* */
私有void updateUI(布尔值isSignedIn){
如果(伊西涅丁){
btnSignIn.setVisibility(View.GONE);
btnSignOut.setVisibility(View.VISIBLE);
btnRevokeAccess.setVisibility(View.VISIBLE);
llProfileLayout.se