-2

Im passing a username and a password to url using basic64 auth method. The response is a token. How can i get this token using volley library ?

Thanks in advance

Narayan C.R
  • 866
  • 1
  • 8
  • 22
  • https://developer.android.com/training/volley/request.html#request-json – BNK Sep 30 '16 at 05:54
  • i think you will find solution here http://stackoverflow.com/questions/31230308/way-to-pass-long-parameter-in-url-request-using-volley Hope this will help you – Vij Sep 30 '16 at 06:04

1 Answers1

-1

You can pass the JsonRequest by using following code.

JsonObjectRequest req = new JsonObjectRequest(Url, new JSONObject(),
                new com.android.volley.Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            Log.v("Response:%n %s", response.toString(0));
                            JSONObject jsonObject = new JSONObject(response.toString());
                            String success = jsonObject.getString("success");

                            // Get your Token Here.

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(LoginActivity.this, "Server or Connection Error.", Toast.LENGTH_SHORT).show();
                            builder.dismiss();
                        }
                    }
                }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.e("Error: ", error.getMessage());
            }
        });

        AppController.getInstance().addToRequestQueue(req);

To pass the request in volley you need AppController class.

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;
    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance(){
        return mInstance;
    }

    public RequestQueue getRequestQueue(){
        if(mRequestQueue == null){
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());

        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }



    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }

}
Piyush k
  • 297
  • 3
  • 14
  • Im sorry..but that is not helping.. im passing the request using parsed network class @Override public Map getHeaders() throws AuthFailureError { Map headers = new HashMap(); // add headers String credentials = email+":"+password; String auth = "Basic "+ Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); headers.put("Authorization", auth); return headers; } – Narayan C.R Sep 30 '16 at 06:11
  • Tried taking the response using the usual method .. but it gives an unknown response.. ResponseData: [B@41a24780 im getting the statuscode correctly while calling response.statuscode.. but while calling response.data, im getting unknown response(ResponseData: [B@41a24780) – Narayan C.R Sep 30 '16 at 06:13