2

I'm using Volley to hit a REST API. Most of my calls return 200, if it returns 400, 401, or 409, I pass back a json response with different error messages as to what's wrong. I've been looking at the parseNetworkRequest, but I'm not really understanding it.

Here's my JsonArrayRequest. It's only supposed to return a status code. No message, which will probably change to adding a json message on 400 or 401

    JsonArrayRequest postVerification = new JsonArrayRequest(url,amountArray,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                realm = Realm.getInstance(getApplicationContext());
                realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        realm.where(AccountsModel.class).beginsWith("actId", accountId).findFirst().setActverified("true");
                    }
                });
                dismissDialog();
                updateView();                    }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("error", "Error: " + error.getMessage());
            dismissDialog();
            Toast message = Toast.makeText(getApplicationContext(),"Unable to verify account at this time", Toast.LENGTH_SHORT);
             message.show();
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            Log.i("Auth", "Bearer " + NetworkEngine.access_token);
            headers.put("Authorization", auth);
            return headers;
        }

        @Override
        public int getMethod() {
            return Method.POST;
        }
        @Override
        protected Response<T> parseNetworkResponse(
                NetworkResponse response) {
            try {
                String json = new String(response.data,
                        HttpHeaderParser.parseCharset(response.headers));
                return Response.success(json,),
                        HttpHeaderParser.parseCacheHeaders(response));
            } catch(Exception e){}

        }
    };

I have an error on the parseNetworkResponse line

error: cannot find symbol
        protected Response<T> parseNetworkResponse(

When I hover over the Response it says it's trying to use an incompatible return type. I really have no idea what to do from here. Not sure what to put in the Response.sucess() or how to actually get the status code. Any help would be appreciated.

Thomas Horner
  • 283
  • 1
  • 2
  • 11
  • This post should help: http://stackoverflow.com/questions/21867929/android-how-handle-message-error-from-the-server-using-volley/21868734#21868734 – Submersed Jun 04 '15 at 18:43
  • Sorry, but Volley has been kicking my butt trying to get it all working When I make the request, I pass GsonRequest variableName = new GsonRequest(url, class,headers, new Response.Listener, new Response.ErrorListener){} the Response.Listener has an error Method 'Annoymous class derived from Listener' must either be declared abstract or implement abstract 'onResponse in 'Listener' Also, how would I override it and add a message body to make it a post request? I'm pretty sure I got it to accept what ever method I pass into it, bit I'm not sure how to add the body part to it. – Thomas Horner Jun 05 '15 at 16:51

2 Answers2

0
  • To get status code from JsonArrayRequest try the following. Hope it helps.

    @Override protected Response parseNetworkResponse(NetworkResponse response) { mStatusCode = response.statusCode; return super.parseNetworkResponse(response); }

NANDWERE
  • 151
  • 2
  • 3
-1

Not Sure what you are trying to do, but if you just wanna get the status code you can use HttpClient.

public JSONArray getJSONFromUrl(String url) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e("==>", "Failed to download file");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Parse String to JSON object
    JSONArray jarray =null;
    try {
        jarray = new JSONArray(builder.toString());
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    } 
    return jarray;
}
smash_apps
  • 72
  • 5