Java 发出以从方法返回int

Java 发出以从方法返回int,java,android,Java,Android,我需要从下面显示的方法中返回一个int变量,但在我的代码中,方法不返回int变量…我不知道为什么 我希望你能帮助我 提前谢谢大家 public int checkLogin(final String emaill, final String password) { // Tag used to cancel the request String tag_string_req = "req_login"; pDialog.setMessag

我需要从下面显示的方法中返回一个
int
变量,但在我的代码中,方法不返回int变量…我不知道为什么

我希望你能帮助我

提前谢谢大家

    public int checkLogin(final String emaill, final String password) {
        // Tag used to cancel the request
        String tag_string_req = "req_login";

        pDialog.setMessage("Logging in ...");
        showDialog();

        StringRequest strReq = new StringRequest(Method.POST,
                AppConfig.URL_LOGIN, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Login Response: " + response.toString());
                hideDialog();

                try {
                    Log.i("ciao1", "["+response+"]");


                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");

                    // Check for error node in json
                    if (!error) {
                        // user successfully logged in
                        // Create login session
                        session.setLogin(true);

                        // Now store the user in SQLite


                        String uid = jObj.getString("uid");
                        id= jObj.getInt("id");
                        System.out.println("BEFORE STORE THE USER IN SQLITE____ID:"+id);

                        idd= jObj.getInt("id");
                        System.out.println("BEFORE STORE THE USER IN SQLITE___ID:"+idd);


                        System.out.println("BEFORE STORE THE USER IN SQLITE ID:"+id);


                        JSONObject user = jObj.getJSONObject("user");

                        String name = user.getString("name");
                        String cognome = user.getString("cognome");

                        String emaill = user.getString("email");
                        String email2 = user.getString("email2");
                        String numero_appartamento = user.getString("numero_appartamento");
                        String nome_edificio = user.getString("nome_edificio");
                        String zona_metropolitana = user.getString("zona_metropolitana");



                        String created_at = user
                                .getString("created_at");
                        Log.i("ciao2", "[" + jObj + "]");

                        // Inserting row in users table
//                        db.addUser(name,cognome, emaill,email2,numero_appartamento,nome_edificio,zona_metropolitana,uid, created_at);
                        db.addUser(name, cognome, emaill, email2, numero_appartamento, nome_edificio, zona_metropolitana, uid, created_at);


                        // Launch main activity
                        Intent intent = new Intent(LoginActivity.this,
//                                MainActivity.class);
                                ScrollableTabsActivity.class);
                        intent.putExtra("EXTRA_SESSION_ID", id);

                        startActivity(intent);
                        finish();

                    } else {
                        // Error in login. Get the error message
                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    // JSON error
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }

            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Login Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
            }

        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting parameters to login url
                Map<String, String> params = new HashMap<String, String>();
                params.put("email", emaill);
                params.put("password", password);
                System.out.println(params);
                return params;
            }

        };

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

        return idd;

    }
public int checkLogin(最终字符串email,最终字符串密码){
//用于取消请求的标记
String tag\u String\u req=“req\u login”;
设置消息(“登录…”);
showDialog();
StringRequest strReq=新的StringRequest(Method.POST,
AppConfig.URL\u登录,新建响应.Listener(){
@凌驾
公共void onResponse(字符串响应){
Log.d(标记“登录响应:+Response.toString());
hideDialog();
试一试{
Log.i(“ciao1”,“[“+响应+”]);
JSONObject jObj=新的JSONObject(响应);
布尔错误=jObj.getBoolean(“错误”);
//检查json中的错误节点
如果(!错误){
//用户已成功登录
//创建登录会话
session.setLogin(true);
//现在将用户存储在SQLite中
stringuid=jObj.getString(“uid”);
id=jObj.getInt(“id”);
System.out.println(“在将用户存储到SQLITE中之前,”+ID);
idd=jObj.getInt(“id”);
System.out.println(“将用户存储在SQLITE中之前,ID:+idd”);
System.out.println(“将用户存储在SQLITE ID中之前:”+ID);
JSONObject user=jObj.getJSONObject(“用户”);
字符串名称=user.getString(“名称”);
String cognome=user.getString(“cognome”);
String emaill=user.getString(“电子邮件”);
String email2=user.getString(“email2”);
String numero_appartamento=user.getString(“numero_appartamento”);
String nome_edificio=user.getString(“nome_edificio”);
String zona_metropolitana=user.getString(“zona_metropolitana”);
在=用户处创建的字符串
.getString(“创建于”);
Log.i(“ciao2”、“[”+jObj+“]”);
//在用户表中插入行
//db.addUser(姓名、cognome、emaill、email2、数字公寓、nome_大厦、zona_metropolitana、uid、创建地址);
db.addUser(姓名、cognome、emaill、email2、数字公寓、nome_大厦、zona_metropolitana、uid、创建地址);
//开展主要活动
意向意向=新意向(LoginActivity.this,
//主要活动(课堂);
ScrollableTabsActivity.class);
intent.putExtra(“额外会话ID”,ID);
星触觉(意向);
完成();
}否则{
//登录时出错。获取错误消息
String errorMsg=jObj.getString(“error_msg”);
Toast.makeText(getApplicationContext(),
errorMsg,Toast.LENGTH_LONG).show();
}
}捕获(JSONException e){
//JSON错误
e、 printStackTrace();
Toast.makeText(getApplicationContext(),“Json错误:+e.getMessage(),Toast.LENGTH_LONG).show();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(标记“登录错误:+Error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(),Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@凌驾
受保护的映射getParams(){
//将参数发布到登录url
Map params=新的HashMap();
参数put(“电子邮件”,emaill);
参数put(“密码”,密码);
系统输出打印项次(参数);
返回参数;
}
};
//将请求添加到请求队列
AppController.getInstance().addToRequestQueue(streq,标记字符串请求);
返回idd;
}

截击请求是异步的,因此当您尝试返回int时,请求可能尚未完成。如果要从截取请求返回值,请使用回调接口

public void getInt(final VolleyCallback callback) {
    StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            callback.onSuccess(<PASS INTEGER RESULT HERE>);
        }
    });
}

public interface VolleyCallback{
    void onSuccess(int result);
}
public StringRequest checkLogin(最终字符串email、最终回调接口回调、最终字符串密码){
String tag\u String\u req=“req\u login”;
设置消息(“登录…”);
showDialog();
StringRequest strReq=新的StringRequest(Method.POST,
AppConfig.URL\u登录,新建响应.Listener(){
@凌驾
公共void onResponse(字符串响应){
Log.d(标记“登录响应:+Response.toString());
hideDialog();
试一试{
Log.i(“ciao1”,“[“+响应+”]);
JSONObject jObj=新的JSONObject(响应);
布尔错误=jObj.getBoolean(“错误”);
//检查json中的错误节点
如果(!错误){
//用户已成功登录
//创建登录会话
session.setLogin(true);
//现在将用户存储在SQLite中
stringuid=jObj.getString(“uid”);
id=jObj.getInt(“id”);
系统
public void onResume(){
    super.onResume();
    getInt(new VolleyCallback(){
         @Override
         public void onSuccess(int result){
             //do stuff here with the int
         }
    });
}
 public StringRequest checkLogin(final String emaill,final CallbackInterface callback, final String password) {

        String tag_string_req = "req_login";

        pDialog.setMessage("Logging in ...");
        showDialog();

        StringRequest strReq = new StringRequest(Method.POST,
                AppConfig.URL_LOGIN, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Login Response: " + response.toString());
                hideDialog();

                try {
                    Log.i("ciao1", "["+response+"]");


                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");

                    // Check for error node in json
                    if (!error) {
                        // user successfully logged in
                        // Create login session
                        session.setLogin(true);

                        // Now store the user in SQLite


                        String uid = jObj.getString("uid");
                        id= jObj.getInt("id");
                        System.out.println("BEFORE STORE THE USER IN SQLITE____ID:"+id);

                        idd= jObj.getInt("id");
                        System.out.println("BEFORE STORE THE USER IN SQLITE___ID:"+idd);


                        System.out.println("BEFORE STORE THE USER IN SQLITE ID:"+id);


                        JSONObject user = jObj.getJSONObject("user");

                        String name = user.getString("name");
                        String cognome = user.getString("cognome");

                        String emaill = user.getString("email");
                        String email2 = user.getString("email2");
                        String numero_appartamento = user.getString("numero_appartamento");
                        String nome_edificio = user.getString("nome_edificio");
                        String zona_metropolitana = user.getString("zona_metropolitana");



                        String created_at = user
                                .getString("created_at");
                        Log.i("ciao2", "[" + jObj + "]");

                        // Inserting row in users table
//                        db.addUser(name,cognome, emaill,email2,numero_appartamento,nome_edificio,zona_metropolitana,uid, created_at);
                        db.addUser(name, cognome, emaill, email2, numero_appartamento, nome_edificio, zona_metropolitana, uid, created_at);


                         callback.onSuccess(idd);//implement this in your MainActivity.java    


                    } else {
                        // Error in login. Get the error message
                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    // JSON error
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }

            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Login Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
            }

        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting parameters to login url
                Map<String, String> params = new HashMap<String, String>();
                params.put("email", emaill);
                params.put("password", password);
                System.out.println(params);
                return params;
            }

        };

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

        return strReq;

    }

public interface CallbackInterface {
    void onSuccess(int result);
}