简体   繁体   English

Okhttp webservice会话在登录后丢失

[英]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. 我在Android中使用Okhttp连接webservice。首先我登录,然后我查询另一个webservice,但我的会话丢失了。 Because of this , webservice return to me "Log in again !" 因此,webservice返回给我“再次登录!” warning. 警告。 Where is the problem ? 问题出在哪儿 ?

Login.java 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 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? 如何使用OkHttp在Android上实现cookie处理?

But attention ! 但要注意! You have to use client and cookiemanager as an instance static and singleton ! 您必须使用client和cookiemanager作为静态和单例实例!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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