2017-05-30 115 views
17

所以,我有這個代碼,以使與凌空POST請求:如何將Cookie會話ID放入抽象請求中?

public class MainActivity extends AppCompatActivity { 

Button btnSearch; 
ProgressDialog loadingDialog; 
ListView lvResult; 
String session_id; 
RequestQueue queue; 
MyCookieManager myCookieManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btnSearch = (Button) findViewById(R.id.btnSearch); 
    lvResult = (ListView) findViewById(R.id.lvResult); 
    loadingDialog = new ProgressDialog(MainActivity.this); 
    loadingDialog.setMessage("Wait.\nLoading..."); 
    loadingDialog.setCancelable(false); 
    myCookieManager = new MyCookieManager(); 

    requestCookie(); //FIRST CALL TO GET SESSION ID 

    btnSearch.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
    showLoading(); 
    requestWithSomeHttpHeaders(); //CALL TO MAKE THE REQUEST WITH VALID SESSION ID 
    } 
    }); 

} 

public void requestCookie() { 
    queue = Volley.newRequestQueue(this); 
    String url = "http://myurl.com/json/"; 

    StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
    new Response.Listener <String>() { 
    @Override 
    public void onResponse(String response) { 
    // 
    String x = myCookieManager.getCookieValue(); 
    } 
    }, 
    new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
    Log.d("ERROR", "Error => " + error.toString()); 
    hideLoading(); 
    } 
    } 
) { 
    @Override 
    public byte[] getBody() throws AuthFailureError { 
    String httpPostBody = "param1=XPTO&param2=XPTO"; 
    return httpPostBody.getBytes(); 
    } 

    @Override 
    public Map < String, String > getHeaders() throws AuthFailureError { 
    Map <String, String> params = new HashMap < String, String >(); 
    params.put("User-Agent", "Mozilla/5.0"); 
    params.put("Accept", "application/json, text/javascript, */*; q=0.01"); 
    params.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
    //params.put("Set-Cookie", session_id);// + " _ga=GA1.3.1300076726.1496455105; _gid=GA1.3.1624400465.1496455105; _gat=1; _gali=AguardeButton"); 
    //"PHPSESSID=ra0nbm0l22gsnl6s4jo0qkqci1"); 
    return params; 
    } 

    protected Response <String> parseNetworkResponse(NetworkResponse response) { 
    try { 
    String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); 
    String header_response = String.valueOf(response.headers.values()); 
    int index1 = header_response.indexOf("PHPSESSID="); 
    int index2 = header_response.indexOf("; path"); 
    //Log.e(Utils.tag, "error is : " + index1 + "::" + index2); 
    session_id = header_response.substring(index1, index2); 

    return Response.success(jsonString, HttpHeaderParser.parseCacheHeaders(response)); 
    } catch (UnsupportedEncodingException e) { 
    return Response.error(new ParseError(e)); 
    } 
    } 
    }; 

    queue.add(postRequest); 
} 

public void requestWithSomeHttpHeaders() { 
    queue = Volley.newRequestQueue(this); 
    String url = "http://myurl.com/json/"; 

    StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
    new Response.Listener <String>() { 
    @Override 
    public void onResponse(String response) { 
    Log.d("Response", response); 
    String x = myCookieManager.getCookieValue(); 
    String status = ""; 

    try { 
     JSONObject resultObject = new JSONObject(response); 
     Log.d("JSON RESULT =>", resultObject.toString()); 
    } catch (JSONException e) { 
     Toast.makeText(MainActivity.this, "Request Error", Toast.LENGTH_SHORT).show(); 
     e.printStackTrace(); 
    } 

    hideLoading(); 
    } 
    }, 
    new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
    Log.d("ERROR", "Error => " + error.toString()); 
    hideLoading(); 
    } 
    } 
) { 
    @Override 
    public byte[] getBody() throws AuthFailureError { 
    String httpPostBody = "param1=XPTO&param2=XPTO"; 
    return httpPostBody.getBytes(); 
    } 

    @Override 
    public Map <String, String> getHeaders() throws AuthFailureError { 
    Map <String, String> params = new HashMap <String, String>(); 
    params.put("User-Agent", "Mozilla/5.0"); 
    params.put("Accept", "application/json, text/javascript, */*; q=0.01"); 
    params.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
    params.put("Cookie", /*myCookieManager.getCookieValue()*/ session_id + "; _ga=GA1.3.1300076726.1496455105; _gid=GA1.3.1624400465.1496455105; _gat=1; _gali=AguardeButton"); 
    return params; 
    } 
    }; 

    queue.add(postRequest); 
} 

private void showLoading() { 
    runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
    if (!loadingDialog.isShowing()) 
    loadingDialog.show(); 
    } 
    }); 
} 

private void hideLoading() { 
    runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
    if (loadingDialog.isShowing()) 
    loadingDialog.dismiss(); 
    } 
    }); 
} 
} 

如果我發送一個有效的cookie ID這個返回一個有效的JSON對象還有一個空的對象。

我試過(不成功)設置默認的cookie處理諸如

CookieManager manager = new CookieManager(); CookieHandler.setDefault(manager);

但我得到一個空對象。

如何將一個有效的cookie會話ID發送到發佈請求?

+0

請參考以下鏈接https://developer.android.com/training/volley/request-custom.html –

+0

@RohitParmar我已經嘗試過實施parseNetworkResponse接收當啓動應用程序然後在請求中使用它時,它不起作用。謝謝。 – PiLHA

+0

你必須看看你的代碼。如果CookieManager不起作用,則必須在您的android版本或實現中出現錯誤。無論如何..如果沒有任何工作,你可以嘗試捕獲「Set-Cookie」標題,每當你做出請求時,服務器響應,存儲它(例如在內存和首選項中)並每次都發送它 –

回答

3

所以,問題是越來越有效的cookie。我的錯誤是從POST請求本身獲取它。我保持相同的工作原理,在啓動應用程序時獲取cookie,但使用GET而不是POST,並調用URL的根目錄而不是獲取JSON的地址。

我的解決辦法是這樣的:

public void requestCookie() { 
    queue = Volley.newRequestQueue(this); 
    String url = "http://myurl.com/"; 

    StringRequest getRequest = new StringRequest(Request.Method.GET, url, 
    new Response.Listener <String>() { 
    @Override 
    public void onResponse(String response) { 
    String x = myCookieManager.getCookieValue(); 
    } 
    }, 
    new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
    Log.d("ERROR", "Error => " + error.toString()); 
    hideLoading(); 
    } 
    } 
) { 
    protected Response <String> parseNetworkResponse(NetworkResponse response) { 
    try { 
    String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); 
    String header_response = String.valueOf(response.headers.values()); 
    int index1 = header_response.indexOf("PHPSESSID="); 
    int index2 = header_response.indexOf("; path"); 
    //Log.e(Utils.tag, "error is : " + index1 + "::" + index2); 
    session_id = header_response.substring(index1, index2); 

    return Response.success(jsonString, HttpHeaderParser.parseCacheHeaders(response)); 
    } catch (UnsupportedEncodingException e) { 
    return Response.error(new ParseError(e)); 
    } 
    } 
    }; 

    queue.add(getRequest); 
} 
0

您在登錄響應中獲取Cookie(會話ID),將cookie保存在sharedpreference或其他數據庫中,並使用它發送請求。

從登錄請求獲得餅乾

CustomStringRequest stringRequest = new CustomStringRequest(Request.Method.POST, SIGN_IN_URL, 
        new Response.Listener<CustomStringRequest.ResponseM>() { 
         @Override 
         public void onResponse(CustomStringRequest.ResponseM result) { 

          CookieManager cookieManage = new CookieManager(); 
          CookieHandler.setDefault(cookieManage); 

          progressDialog.hide(); 
          try { 
           //From here you will get headers 
           String sessionId = result.headers.get("Set-Cookie"); 
           String responseString = result.response; 

           Log.e("session", sessionId); 
           Log.e("responseString", responseString); 

           JSONObject object = new JSONObject(responseString); 

CustomStringRequest類

public class CustomStringRequest extends Request<CustomStringRequest.ResponseM> { 


    private Response.Listener<CustomStringRequest.ResponseM> mListener; 

    public CustomStringRequest(int method, String url, Response.Listener<CustomStringRequest.ResponseM> responseListener, Response.ErrorListener listener) { 
     super(method, url, listener); 
     this.mListener = responseListener; 
    } 


    @Override 
    protected void deliverResponse(ResponseM response) { 
     this.mListener.onResponse(response); 
    } 

    @Override 
    protected Response<ResponseM> parseNetworkResponse(NetworkResponse response) { 
     String parsed; 
     try { 
      parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); 
     } catch (UnsupportedEncodingException e) { 
      parsed = new String(response.data); 
     } 

     ResponseM responseM = new ResponseM(); 
     responseM.headers = response.headers; 
     responseM.response = parsed; 

     return Response.success(responseM, HttpHeaderParser.parseCacheHeaders(response)); 
    } 


    public static class ResponseM { 
     public Map<String, String> headers; 
     public String response; 
    } 

} 

設定cookie時添加請求到服務器..

   @Override 
       public Map<String, String> getHeaders() throws AuthFailureError { 
        Map<String, String> headers = new HashMap<String, String>(); 

        String session=sharedPreferences.getString("sessionId",""); 
        headers.put("Cookie",session); 
        return headers; 
       } 
+0

你測試了你的答案嗎?它有用嗎?如前所述,在評論中說過,我試圖在啓動應用程序時發出請求來捕獲cookie會話並在另一個請求中使用它,但它不起作用。我會嘗試使用共享偏好設置,但我認爲不應區分最終結果。 – PiLHA

+0

此代碼在我的項目中,它的工作是100%,請更新您的問題代碼如何實現,然後我將發佈所有代碼與更改.. –

+0

我更新了我的當前代碼的問題。 – PiLHA

1

Using cookies with Android volley library

請求類:

public class StringRequest extends com.android.volley.toolbox.StringRequest { 

    private final Map<String, String> _params; 

    /** 
    * @param method 
    * @param url 
    * @param params 
    *   A {@link HashMap} to post with the request. Null is allowed 
    *   and indicates no parameters will be posted along with request. 
    * @param listener 
    * @param errorListener 
    */ 
    public StringRequest(int method, String url, Map<String, String> params, Listener<String> listener, 
      ErrorListener errorListener) { 
     super(method, url, listener, errorListener); 

     _params = params; 
    } 

    @Override 
    protected Map<String, String> getParams() { 
     return _params; 
    } 

    /* (non-Javadoc) 
    * @see com.android.volley.toolbox.StringRequest#parseNetworkResponse(com.android.volley.NetworkResponse) 
    */ 
    @Override 
    protected Response<String> parseNetworkResponse(NetworkResponse response) { 
     // since we don't know which of the two underlying network vehicles 
     // will Volley use, we have to handle and store session cookies manually 
     MyApp.get().checkSessionCookie(response.headers); 

     return super.parseNetworkResponse(response); 
    } 

    /* (non-Javadoc) 
    * @see com.android.volley.Request#getHeaders() 
    */ 
    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
     Map<String, String> headers = super.getHeaders(); 

     if (headers == null 
       || headers.equals(Collections.emptyMap())) { 
      headers = new HashMap<String, String>(); 
     } 

     MyApp.get().addSessionCookie(headers); 

     return headers; 
    } 
} 

MyApp的:

public class MyApp extends Application { 
    private static final String SET_COOKIE_KEY = "Set-Cookie"; 
    private static final String COOKIE_KEY = "Cookie"; 
    private static final String SESSION_COOKIE = "sessionid"; 

    private static MyApp _instance; 
    private RequestQueue _requestQueue; 
    private SharedPreferences _preferences; 

    public static MyApp get() { 
     return _instance; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     _instance = this; 
      _preferences = PreferenceManager.getDefaultSharedPreferences(this); 
     _requestQueue = Volley.newRequestQueue(this); 
    } 

    public RequestQueue getRequestQueue() { 
     return _requestQueue; 
    } 


    /** 
    * Checks the response headers for session cookie and saves it 
    * if it finds it. 
    * @param headers Response Headers. 
    */ 
    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

你測試了你的答案嗎?有用? – PiLHA