Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.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
Java Android-从AsyncTask doInBackground函数返回整数值_Java_Android_Android Asynctask_Sharedpreferences - Fatal编程技术网

Java Android-从AsyncTask doInBackground函数返回整数值

Java Android-从AsyncTask doInBackground函数返回整数值,java,android,android-asynctask,sharedpreferences,Java,Android,Android Asynctask,Sharedpreferences,已解决 谢谢你们,我通过从AsyncTask函数中删除PreExecute函数解决了我的问题。谢谢大家 我想从一个AsycntTask函数返回一个整数值,这个整数值是用户ID。我试着用谷歌搜索我的问题,看到了很多关于stackoverflow的讨论,但没有一个解决了我的问题。希望你们能指导我怎么做 最终代码:(解决问题后) 私有类ValidateUserAccount扩展异步任务{ 字符串响应=”; @凌驾 受保护的空位背景(空位…空位){ HashMap loginDataParams=新Ha

已解决 谢谢你们,我通过从AsyncTask函数中删除PreExecute函数解决了我的问题。谢谢大家

我想从一个AsycntTask函数返回一个整数值,这个整数值是用户ID。我试着用谷歌搜索我的问题,看到了很多关于stackoverflow的讨论,但没有一个解决了我的问题。希望你们能指导我怎么做

最终代码:(解决问题后)

私有类ValidateUserAccount扩展异步任务{
字符串响应=”;
@凌驾
受保护的空位背景(空位…空位){
HashMap loginDataParams=新HashMap();
LoginDaparams.put(“电子邮件”,strEmail);
loginDataParams.put(“密码”,strPassword);
JSONObject jsonData=新的JSONObject(后勤数据库);
字符串path=“myUrl”;
response=Login.ServerData(路径,jsonData);
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
uProgressBar.setVisibility(View.GONE);
int code=Login.passResCode();
userId=Login.passUserId();
如果(代码==200){
Toast.makeText(getApplicationContext(),“成功登录:”+userId,Toast.LENGTH\u SHORT.show();
userIdSharedPreferences=PreferenceManager.GetDefaultSharedReferences(LoginActivity.this);
Intent loginsaccess=新的Intent(getApplicationContext(),MainActivity.class);
userIdEditor=userIdSharedPreferences.edit();
putInt(“userId”,userId);
userIdEditor.apply();
startActivity(登录成功);
完成();
}否则{
final AlertDialog.Builder alertBox=新建AlertDialog.Builder(LoginActivity.this);
字符串errorMessage=“”;
如果(代码==401)
errorMessage=“电子邮件和密码无效。”;
else if(代码==0)
errorMessage=“请检查您的internet连接。”;
alertBox.setMessage(errorMessage).setCancelable(false)
.setNegativeButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
公共void onClick(DialogInterface,inti){
dialogInterface.cancel();
}
});
AlertDialog alert=alertBox.create();
setTitle(“哎呀,出了点问题!”);
alert.show();
}
}
}

我想你可以把它作为额外内容放在意向书中。。。我真的不明白你为什么使用userIdEditor,它是什么。。。但我认为这样做会奏效

Intent loginsaccess=newintent(getApplicationContext(),MainActivity.class);
loginsAccess.putExtra(“userId”,userId);
startActivity(登录成功);
您可以通过执行以下操作在打开活动中重新获得id


int userId=getIntent().getExtra().getInt(“userId”)

您可以使用
Intent#putExtra
[Read](,android.os.Bundle))在活动之间传递额外信息,更多信息请参见此处

写入SharedReference时,请使用userIdEditor.commit();提交函数将返回布尔值,因此您可以检查SharedReference值是否已提交或是否已成功应用

从中您可以了解如何在AsyncTask中更新值您可以显示如何定义
userIdSharedPreferences
userIdEditor
以及如何访问数据库中的值吗
main活动
?尝试此操作,。。。它显示了红线,上面写着:无法解析方法“putExtras(java.lang.String,java.lang.Integer)”,我正在AsycntTask Function中执行此操作。最终得到了所有。。。您可以使用intent的附加功能,并在启动活动时将其保存在SharedReferences中验证方法名称。。你有没有在评论中写过类似的附加内容?如果您删除了's'
private class ValidateUserAccount extends AsyncTask<Void, Integer, Void> {
    String response = "";

    @Override
    protected Void doInBackground(Void... voids) {
        HashMap<String, String> loginDataParams = new HashMap<>();
        loginDataParams.put("email", strEmail);
        loginDataParams.put("password", strPassword);
        JSONObject jsonData = new JSONObject(loginDataParams);
        String path = "myUrl";
        response = Login.ServerData(path, jsonData);
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        uProgressBar.setVisibility(View.GONE);
        int code = Login.passResCode();
        userId = Login.passUserId();
        if (code == 200) {
            Toast.makeText(getApplicationContext(), "Successfully Login: " + userId, Toast.LENGTH_SHORT).show();
            userIdSharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
            Intent loginSuccess = new Intent(getApplicationContext(), MainActivity.class);
            userIdEditor = userIdSharedPreferences.edit();
            userIdEditor.putInt("userId", userId);
            userIdEditor.apply();
            startActivity(loginSuccess);
            finish();
        } else {
            final AlertDialog.Builder alertBox = new AlertDialog.Builder(LoginActivity.this);
            String errorMessage = "";
            if (code == 401)
                errorMessage = "Email and Password are not valid.";
            else if (code == 0)
                errorMessage = "Please check your internet connection.";

            alertBox.setMessage(errorMessage).setCancelable(false)
                    .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.cancel();
                        }
                    });
            AlertDialog alert = alertBox.create();
            alert.setTitle("Oops, Something went wrong!");
            alert.show();
        }
    }
}