排球获取请求和参数

时间:2016-02-22 11:59:23

标签: android android-volley

我试图点击一个网址来获取Json响应以前我使用HTTPURLConnection并且它工作得非常好所以我从HttpUrlConnection更新为Volley

我想要点击的网址是

   http://162.13.137.145:8073/api/PageContent/GetPageContentsByName?PageName=About Us

但是对于Volley,我无法得到回应 我已经从请求扩展了自定义类,但我遇到了以下问题

error: org.json.JSONEXception: Value Access of type java.lang.String cannot be converted to JSONObject

自定义类的代码如下

 public class CustomGetPostRequest extends Request<JSONObject> {
    private int mMethod;
    private String mUrl;
    Map<String, String> mParams= new HashMap<String ,String>();
    private Response.Listener<JSONObject> mListener;
    HashMap<String, String> headers = new HashMap<String, String>();

    public CustomGetPostRequest(int method, String url, Map<String, String> params,
                         Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        mMethod = method;
        mUrl = url;
        Log.d("Main URL",mUrl);
        mParams = params;
        mListener = reponseListener;
    }

    @Override
    public String getUrl() {
        if(mMethod == Request.Method.GET) {
            StringBuilder stringBuilder = new StringBuilder(mUrl);
            Iterator<Map.Entry<String, String>> iterator = mParams.entrySet().iterator();
            int i = 1;
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                if(i == 1) {
                    stringBuilder.append("?" + entry.getKey() + "=" + entry.getValue());
                } else {
                    stringBuilder.append("&" + entry.getKey() + "=" + entry.getValue());
                }
                iterator.remove(); // avoids a ConcurrentModificationException
                i++;
            }
            mUrl = stringBuilder.toString();
            Log.d("Converted URL",mUrl);
        }
        return mUrl;
    }

    @Override
    protected Map<String, String> getParams()
            throws com.android.volley.AuthFailureError {
           Log.d("getParams","Called");
        return mParams;
    }
    @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 ("Content-Type", "application/x-www-form-urlencoded");
        return headers;
    }
    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {

            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            if (response.statusCode == 200)// Added for 200 response
                return Response.success(new JSONObject(),HttpHeaderParser.parseCacheHeaders(response));
            else
            return Response.error(new ParseError(je));
        }
    }

    @Override
    protected void deliverResponse(JSONObject response) {
        // TODO Auto-generated method stub
        mListener.onResponse(response);
    }
}

我也知道,对于请求类型Get getParam()未调用,因此更改了url并在url中直接添加了params 关于排球的请求正在关注

  CustomGetPostRequest getPostRequest = new CustomGetPostRequest(Request.Method
                .GET,cachedURL,params,this,this);
        getPostRequest.setTag(ResponseTag.CONTENT_PAGES);

        mQueue.add(getPostRequest);

我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

长时间调试后我遇到的唯一问题是url被空间推送而未被识别为正确的URL因此我更改了网址

 http://162.13.137.145:8073/api/PageContent/GetPageContentsByName?PageName=About Us

http://162.13.137.145:8073/api/PageContent/GetPageContentsByName?PageName=About%20Us

并且有效