我需要从下面显示的方法返回一个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;
}
答案 0 :(得分:0)
Volley请求是异步的,所以当你尝试返回一个int时,请求可能还没有完成。如果要从Volley请求返回值,请使用回调接口。
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 void onResume(){
super.onResume();
getInt(new VolleyCallback(){
@Override
public void onSuccess(int result){
//do stuff here with the int
}
});
}
答案 1 :(得分:0)
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);
}