简体   繁体   中英

JSON request using Volley

Im trying to make a JSON request using Volley, i was able to successfully make a request using StringRequest but now have an error when trying to do a JSONRequest.

private void postData(final String param, final TextView tv) {
    RequestQueue request = Volley.newRequestQueue(this);

    JsonObjectRequest postReq = new JsonObjectRequest(Request.Method.GET, url_login, new Response.Listener<JsonReader>() {
        @Override
        public void onResponse(JsonReader response) {
            tv.setText(response); // We set the response data in the TextView
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("Error [" + error + "]");
        }
    }) {
        /**
         * Add the headers to the request
         * @return headers
         * @throws AuthFailureError
         */
        @Override
        public Map getHeaders() throws AuthFailureError {
            Map headers = new HashMap();
            headers.put("customHeader", "someCrap");
            System.out.println(headers); //testing output of headers
            return headers;
        }
    };
    request.add(postReq);
}

Im getting an error under the tv.setText(response);

Cannot resolve method 'setText(android.util.JsonReader)'

I would like to output the Json request as a test to text in the TextView labelled "tv"

Thank you

TextView does not accept JsonReader. You need CharSequence or String for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM