2017-04-25 77 views
0

我做了一個項目,並使用REST來發送和從服務器獲取數據,我用HttpURLConnection的發送請求發送請求和響應拿到餅乾與Android凌空

然後我發現抽射,使其更易於使用,但我在使用上凌空

這裏餅乾的一個問題是我的請求函數

public void doActionJsonPost() { 
    // Request a string response from the provided URL. 
    StringRequest stringRequest = new StringRequest(Request.Method.POST, IConstants.BASE_URL + url, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        JSONObject jsonObject; 
        try { 
         jsonObject = new JSONObject(response); 
         String msgCode = jsonObject.getString("responseCode"); 
        } catch (JSONException e) { 
         LoggingHelper.verbose(e.toString()); 
         iHttpAsync.onAsyncFailed(e.toString()); 
        } catch (Exception e) { 
         LoggingHelper.verbose(e.toString()); 
         iHttpAsync.onAsyncFailed(e.toString()); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        LoggingHelper.verbose("ERROR"); 
        iHttpAsync.onAsyncFailed(error.getMessage()); 
       } 
      }){ 
     @Override 
     public byte[] getBody() throws AuthFailureError { 
      iHttpAsync.onAsyncProgress(); 
      return parameters.getBytes(); 
     } 
     @Override 
     public String getBodyContentType() { 
      return "application/json"; 
     } 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      return getAuthHeader(context); 
     } 

     @Override 
     protected Response<String> parseNetworkResponse(NetworkResponse response) { 
      Map<String, String> responseHeaders = response.headers; 
      String rawCookies = responseHeaders.get("Set-Cookie"); 
      return super.parseNetworkResponse(response); 
     } 
    }; 

    RequestQueue requestQueue = Volley.newRequestQueue(context); 
    requestQueue.add(stringRequest); 
} 

我不得不重寫parseNetworkResponse獲得headerResponse,但我看不出有餅乾

我的問題是,我怎樣才能發送和從凌空餅乾?

回答

0

試試這個,並通過你的頭在這個方法

public final void checkSessionCookie(Map<String, String> headers) { 
      if (headers.containsKey(SET_COOKIE_KEY) 
        && headers.get(SET_COOKIE_KEY).startsWith(SESSION_COOKIE)) { 
        String cookie = headers.get(SET_COOKIE_KEY); 
        if (cookie.length() > 0) { 
         String[] splitCookie = cookie.split(";"); 
         String[] splitSessionId = splitCookie[0].split("="); 
         cookie = splitSessionId[1]; 
         Editor prefEditor = _preferences.edit(); 
         prefEditor.putString(SESSION_COOKIE, cookie); 
         prefEditor.commit(); 
        } 
       } 
     } 

     /** 
     * Adds session cookie to headers if exists. 
     * @param headers 
     */ 
     public final void addSessionCookie(Map<String, String> headers) { 
      String sessionId = _preferences.getString(SESSION_COOKIE, ""); 
      if (sessionId.length() > 0) { 
       StringBuilder builder = new StringBuilder(); 
       builder.append(SESSION_COOKIE); 
       builder.append("="); 
       builder.append(sessionId); 
       if (headers.containsKey(COOKIE_KEY)) { 
        builder.append("; "); 
        builder.append(headers.get(COOKIE_KEY)); 
       } 
       headers.put(COOKIE_KEY, builder.toString()); 
      } 
     } 
+0

我在哪裏調用此方法?以及SET_COOKIE_KEY,SESSION_COOKIE,SESSION_COOKIE,COOKIE_KEY,_preferences是什麼? 對不起,我是新的凌空庫 –