如何获取响应数据表格凌乱解析http标头请求?

时间:2016-09-30 05:39:34

标签: android android-volley

我使用basic64 auth方法将用户名和密码传递给url。响应是一个令牌。如何使用齐射库获取此令牌?

提前致谢

1 个答案:

答案 0 :(得分:-1)

您可以使用以下代码传递JsonRequest。

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);

要以截击方式传递请求,您需要AppController类。

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);
        }
    }

}