简体   繁体   中英

Okhttp webservice session lost after login

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

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM