0

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

Ziem
  • 6,579
  • 8
  • 53
  • 86
Stillie
  • 2,647
  • 6
  • 28
  • 50

1 Answers1

2

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

inmyth
  • 8,880
  • 4
  • 47
  • 52
  • is there a way i can display the Json as String on the device? or does it have to be parsed? – Stillie May 14 '15 at 09:23
  • I think you do something weird. Why do you initiate StringRequest with JsonObjectRequest ? Just do `StringRequest postReq = new StringRequest(...)` and replace JsonReader in `onResponse` with String. – inmyth May 14 '15 at 09:26
  • sorry it was supposed to be JsonObjectRequest postReq = new JsonObjectRequest(...) i have changed it in the question. regarding your suggestion, i would like to do a Json request as apposed to a xml(String) request. I managed to get a successful String request from the server which is also posting Json, and would rather use Json. I just am battling to pass the response to be able to see the output on the device? – Stillie May 14 '15 at 09:34
  • First of all, I doubt you want to print the whole json response (there is no xml involved here). What you want to do is print a certain element in that json in a textview. That said, it doesn't matter whether you use JsonObjectRequest or StringRequest. You just need Gson to instantly transform the response either JsonReader or String to an object. You just need to define the json data model. This will help you: http://stackoverflow.com/questions/2864370/how-do-i-use-googles-gson-api-to-deserialize-json-properly – inmyth May 14 '15 at 09:51