1

I am trying to send some requests to an API developed in Symfony.

I connect through it using this :

   if (param.getRequestType().equals("post")) {
            HttpPost httppost = new HttpPost(url);
            try {
                httppost.setEntity(new UrlEncodedFormEntity(param.getParam()));
                HttpResponse res = httpclient.execute
                        (httppost);
                return EntityUtils.toString(res.getEntity());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

It works well and the API return that I am connected.

Then I want to use an other API request (get) but it seems that my session is not saved and it never works.

The get request looks like this :

@Override
    protected String doInBackground(String... uri) {
        CookieStore cookieStore = new BasicCookieStore();
        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://***.**/***/faq");
        String responseString = null;
        try {
            HttpResponse response = httpclient.execute(httpget, localContext);
            StatusLine statusLine = response.getStatusLine();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            responseString = out.toString();
            System.out.println("HERE --> " + responseString);
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
    }

I think this is happening because Android doesn't keep my session alive. Am I right ? How can I keep it ?

Symfony is using Fosuserbundle for the login part.

ZeenaZeek
  • 271
  • 3
  • 5
  • 16
  • http://stackoverflow.com/questions/3587254/how-do-i-manage-cookies-with-httpclient-in-android-and-or-java – Matteo Apr 14 '16 at 19:49
  • I edited my question : I modified the get request to use the CookieStore. But I still have the exact same issue – ZeenaZeek Apr 24 '16 at 20:23

0 Answers0