使用Volley

时间:2016-06-28 17:33:49

标签: android https android-volley

我想要使用https url请求web服务,但会发生Volley错误(NoConnectionError)。 我认为问题是https url,但我没有在互联网上找到任何解决方案。 有谁可以帮助我?

错误是:" com.android.volley.NoConnectionError:java.io.IOException:未找到身份验证质询"

代码:

    RequestQueue queue = VolleySingleton.getInstance(this).getRequestQueue();

    String url = "https://www.websiteco.com/rest/index.php/login/user/email/ana@gmail.com/password/123456";

    JsonObjectRequest json = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            String s = response.toString();
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            String errorStr = "";

            if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                errorStr = "TimeoutError or NoConnectionError";
            } else if (error instanceof AuthFailureError) {
                errorStr = "AuthFailureError";
            } else if (error instanceof ServerError) {
                errorStr = "ServerError";
            } else if (error instanceof NetworkError) {
                errorStr = "NetworkError";
            } else if (error instanceof ParseError) {
                errorStr = "ParseError";
            }

            Toast.makeText(getApplicationContext(),errorStr,Toast.LENGTH_LONG).show();

        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> parms = new HashMap<>();
            parms.put("Key-Api", "*******");
            return parms;
        }

        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
            try {
                String jsonString = new String(response.data,
                        HttpHeaderParser.parseCharset(response.headers));
                return Response.success(new JSONObject(jsonString),
                        HttpHeaderParser.parseCacheHeaders(response));
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }
        }
    };

    queue.add(json);

0 个答案:

没有答案