当我尝试使用volley访问URL时,它说403禁止,但是当使用浏览器时它返回JSON数据

时间:2016-11-22 06:51:52

标签: android android-volley

这是我的代码,我的api基本网址为http://mydomail.com/mp3/mobile_app_services.php

返回以下错误 BasicNetwork.performRequest:http://mydomail.com/mp3/mobile_app_services.php

的意外响应代码403
    CommonUtils.showProgressDialog(context);
    Map<String, String> map = new HashMap<>();
    map.put("action", ConstantUtils.ACTION_NAME_LATEST_TRACK);
    map.put("userId", AppInfo.getKeyUserId(context));
    map.put("auth_code", AppInfo.getKeyAuthCode(context));

    CustomRequest customRequest = new CustomRequest(Request.Method.POST, ConstantUtils.API_BASE_URL, map, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            CommonUtils.cancelProgressDialog();

            int code = -1;
            String message;

            if (response != null) {

                try {
                    Log.d("","response#"+response.toString());
                    code = response.getInt("code");
                    message = response.getString("message");

                    if (code == 200) {

                        JSONArray jsonArray = response.getJSONArray("data");
                        LatestTrackModel latestTrackModel;
                        for (int i = 0; i < jsonArray.length(); i++) {


                            JSONObject data = jsonArray.getJSONObject(i);
                            String songId = data.getString("songId");
                            String songTitle = data.getString("songTitle");
                            String description = data.getString("description");
                            String movieId = data.getString("movieId");
                            String movieTitle = data.getString("movieTitle");
                            String movieDescription = data.getString("movieDescription");
                            String movieImage = data.getString("movieImage");
                            String releaseDate = data.getString("releaseDate");
                            String songDurationInMunites = data.getString("songDurationInMunites");
                            String artistName = data.getString("artistName");
                            String fileSizeInKbs = data.getString("fileSizeInKbs");
                            String trackImage = data.getString("trackImage");
                            String songUrl = data.getString("songUrl");
                            long contentLength = data.optInt("content-length", -1);

                            latestTrackModel = new LatestTrackModel(songId, songTitle, description, movieId, movieTitle, movieDescription, movieImage, releaseDate, trackImage, songDurationInMunites, artistName, fileSizeInKbs, songUrl, contentLength);
                            arrayList.add(latestTrackModel);
                        }


                        rvLatestTrack.setAdapter(new LatestTrackAdapter(context, arrayList));
                    } else {

                        CommonUtils.showToast(context, message);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            } else {

                CommonUtils.showToast(context, getString(R.string.server_response));
            }

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

            // As of f605da3 the following should work
            NetworkResponse response = error.networkResponse;
            if (error instanceof ServerError && response != null) {
                try {
                    String res = new String(response.data,
                            HttpHeaderParser.parseCharset(response.headers, "utf-8"));
                    // Now you can use any deserializer to make sense of data

                    Log.d("","responce11#"+res);
                    JSONObject obj = new JSONObject(res);
                } catch (UnsupportedEncodingException e1) {
                    // Couldn't properly decode data to string
                    e1.printStackTrace();
                } catch (JSONException e2) {
                    // returned data is not JSONObject?
                    e2.printStackTrace();
                }
            }

        }
    }){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            headers.put("User-agent", System.getProperty("http.agent"));
            return headers;
        }
    };

    customRequest.setRetryPolicy(new DefaultRetryPolicy(ConstantUtils.SOCKET_TIME_OUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    VolleySingleton.getInstance(context).addToRequestQueue(customRequest);

0 个答案:

没有答案