关于jsonobjectrequest和jsonarrayrequest的Volley状态代码和消息返回

时间:2015-06-04 18:25:14

标签: android android-volley

我使用Volley来点击REST API。我的大多数调用返回200,如果它返回400,401或409,我会传回一个json响应,其中包含错误消息,指出错误。我一直在看parseNetworkRequest,但我并没有真正理解它。

这是我的JsonArrayRequest。它只应该返回一个状态代码。没有消息,可能会更改为在400或401上添加json消息

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

        }
    };

我在parseNetworkResponse行上有错误

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

当我将鼠标悬停在“响应”上时,它表示它正在尝试使用不兼容的返回类型。我真的不知道该怎么做。不确定要放在Response.sucess()中的内容或如何实际获取状态代码。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

  • 要从JsonArrayRequest获取状态代码,请尝试以下操作。希望对您有所帮助。

    @Override 受保护的响应parseNetworkResponse(NetworkResponse response){ mStatusCode = response.statusCode; 返回super.parseNetworkResponse(response); }

答案 1 :(得分:-1)

不确定您要做什么,但如果您只想获取状态代码,则可以使用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;
}