I am new to Android and Volley, and I need your help. I need to post a String, have a json to response to it and, if it doesn't raise a bad request, start a new intent with some values coming from my request.
This is a simple schema of what I want to do: Press Login button -> star request -> check if it is ok -> start a new intent with response values.
I have seen that Volley uses asynchronous method to query. Here's my code:
boolean temp=false;
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean temp=false;
if (!id.getText().toString().isEmpty() && !pw.getText().toString().isEmpty()) {
temp = verifyCredentials(v.getContext()); //Doesn't work because Volley is asynchronous.
if(temp==true)
{
Intent intentMain = new Intent(v.getContext(), MainActivity.class);//MainActivity.class);
intentMain.putExtra("username", id.getText().toString());
startActivityForResult(intentMain, 0);
}
} else {//strighe vuote
//toast
Toast.makeText(v.getContext(), "Compila i campi", Toast.LENGTH_SHORT).show();
}
}
});
public boolean verifyCredentials(Context context) {
final boolean[] tempToReturn = {false};
mTextView = (TextView) findViewById(R.id.textView2);
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest stringRequest = new StringRequest(Request.Method.POST, apiURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mTextView.setText("Response is:" + response.substring(500));
tempToReturn[0] =true;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String json = null;
NetworkResponse response = error.networkResponse;
if(response != null && response.data != null){
switch(response.statusCode){
case 400:
json = new String(response.data);
json = trimMessage(json, "message");
if(json != null) displayMessage(json);
break;
}
//Additional cases
}
mTextView.setText("Error bad request");
}
public String trimMessage(String json, String key){
String trimmedString = null;
try{
JSONObject obj = new JSONObject(json);
trimmedString = obj.getString(key);
} catch(JSONException e){
e.printStackTrace();
return null;
}
return trimmedString;
}
//Somewhere that has access to a context
public void displayMessage(String toastString){
mTextView.setText("Response is:" +toastString);
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
AuthenticationUserName = id.getText().toString();
AuthenticationPassword = pw.getText().toString();
params.put("grant_type", Authenticationgrant_type);
params.put("username", AuthenticationUserName);
params.put("password", AuthenticationPassword);
return params;
}
};
queue.add(stringRequest);
return tempToReturn[0];
}
I am using Volley because my gradle's is the 23 and my APi level too so I can't use the apache package.
EDIT: NEW CODE:
public void onClick(View v) {
boolean temp = true;
if (!id.getText().toString().isEmpty() && !pw.getText().toString().isEmpty()) {
myContext = v.getContext();
VolleyResponseListener listener = new VolleyResponseListener() {
@Override
public void onError(VolleyError error) {
String json = null;
NetworkResponse response = error.networkResponse;
if(response != null && response.data != null){
switch(response.statusCode){
case 400:
json = new String(response.data);
json = trimMessage(json, "message");
if(json != null) displayMessage(json);
break;
}
//Additional cases
}
mTextView.setText("Error bad request");
}
@Override
public void onResponse(JSONObject response) {
try {
fullName = response.getString("fullName");
token= response.getString("access_token");
expirationDate=response.getString(".expires");
} catch (JSONException e) {
e.printStackTrace();
}
mTextView.setText("Response is:" + fullName+token+expirationDate);
Intent intentMain = new Intent(myContext, MainActivity.class);//MainActivity.class);
intentMain.putExtra("username", id.getText().toString());
startActivityForResult(intentMain, 0);
}
public String trimMessage(String json, String key){
String trimmedString = null;
try{
JSONObject obj = new JSONObject(json);
trimmedString = obj.getString(key);
} catch(JSONException e){
e.printStackTrace();
return null;
}
return trimmedString;
}
//Somewhere that has access to a context
public void displayMessage(String toastString){
//Toast.makeText(MainActivity.this, toastString, Toast.LENGTH_LONG).show();
mTextView.setText("Response is:" +toastString);
}
};
verifyCredentials(myContext,listener);
and I have create this interface:
public interface VolleyResponseListener {
void onError(VolleyError error);
void onResponse(JSONObject response);
}
And here is the new code of my verifycredential:
public boolean verifyCredentials(Context context,final VolleyResponseListener listener) {
final boolean[] tempToReturn = {false};
mTextView = (TextView) findViewById(R.id.textView2);
Map<String,String> params = new HashMap<String, String>();
AuthenticationUserName = id.getText().toString();
AuthenticationPassword = pw.getText().toString();
//key value
params.put("grant_type", Authenticationgrant_type);
params.put("username", AuthenticationUserName);
params.put("password", AuthenticationPassword);
RequestQueue queue = Volley.newRequestQueue(context);
SimpleRequest jsObjRequest = new SimpleRequest(Request.Method.POST, apiURL,
params,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
listener.onResponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.onError(error);
}
}
});
queue.add(jsObjRequest);
return tempToReturn[0];
}