2016-02-22 129 views
0

我想打一個網址,以獲得JSON響應以前我是用HttpURLConnection的,其工作完全正常,所以我從HttpURLConnection的更新凌空凌空用GET請求和參數

我試圖打的網址是繼

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

但凌空我無法得到響應 我做了一個自定義類的請求延長,但我有以下問題

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

我也才知道,對於請求類型獲取getParam()不叫這麼改的URL,並直接加入網址參數爲
請求凌空如下

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

     mQueue.add(getPostRequest); 

我在這裏失蹤了什麼?

回答

2

,我是長調試後面臨的唯一問題是URL是用空間推不承認一個正確的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 

和它的工作

+0

是。 URL編碼您的URL字符串 –