3

I'm connecting webservice by using Okhttp in Android.Firstly I login, and after this, I query another webservice but my session lost. Because of this , webservice return to me "Log in again !" warning. Where is the problem ?

Login.java

public class QueryWord {
    public static final MediaType JSON
            = MediaType.parse("application/json; charset=UTF-8");     

    JSONObject run(String url, String level) throws IOException, JSONException {

        try {  
            RequestBody body = new FormEncodingBuilder()
                    .add("mode", level).build();
            Request request;

                request = new Request.Builder()
                        .url(url)
                        .post(body)
                        .build();

            Response responses = null;

            try {
                responses = CommonClient.getClient().newCall(request).execute();

            } catch (IOException e) {
                e.printStackTrace();
            }
            String jsonData = responses.body().string();

            return new JSONObject(jsonData);

        }
        catch (JSONException error){
            Log.e("JSON Error : ", error.getMessage());
            return null;
        }   
    }   
}

OueryWord.java

public class QueryWord {

    public static final MediaType JSON
            = MediaType.parse("application/json; charset=UTF-8");

  JSONObject run(String url, String level) throws IOException, JSONException {  
        try { 
            RequestBody body = new FormEncodingBuilder()
                    .add("mode", level).build();
            Request request;

                request = new Request.Builder()
                        .url(url)
                        .post(body)
                        .build();

            Response responses = null;

            try {
                responses = CommonClient.getClient().newCall(request).execute();

            } catch (IOException e) {
                e.printStackTrace();
            }
            String jsonData = responses.body().string();

            return new JSONObject(jsonData);

        }
        catch (JSONException error){
            Log.e("JSON Error : ", error.getMessage());
            return null;
        }   
    }
}
hkaraoglu
  • 1,345
  • 1
  • 15
  • 34

1 Answers1

0

Ok Guys. I found the solution this link .

How to implement cookie handling on Android using OkHttp?

But attention ! You have to use client and cookiemanager as an instance static and singleton !

Community
  • 1
  • 1
hkaraoglu
  • 1,345
  • 1
  • 15
  • 34