zoukankan      html  css  js  c++  java
  • android下asynchttp库对于session的支持

    默认asynchttp库不支持session,需要用户配置下cookie来处理,直接贴支持session的代码

    package example.com.sessiontest;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    import com.loopj.android.http.AsyncHttpClient;
    import com.loopj.android.http.AsyncHttpResponseHandler;
    import com.loopj.android.http.PersistentCookieStore;
    
    import cz.msebera.android.httpclient.Header;
    import cz.msebera.android.httpclient.client.CookieStore;
    import cz.msebera.android.httpclient.client.protocol.ClientContext;
    import cz.msebera.android.httpclient.cookie.Cookie;
    import cz.msebera.android.httpclient.protocol.HttpContext;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button testButton = (Button)findViewById(R.id.button);
            testButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    AsyncHttpClient client = new AsyncHttpClient();
    
                    HttpContext httpContext = client.getHttpContext();
                    CookieStore cookies = (CookieStore) httpContext.getAttribute(ClientContext.COOKIE_STORE);//获取AsyncHttpClient中的CookieStore
                    if(cookies!=null){
                        for(Cookie c:cookies.getCookies()){
                            System.out.println("main before ~~" + c.getName() + c.getValue());
                        }
                    }else{
                        System.out.println("main  before~~" + "cookies is null");
                    }
                    PersistentCookieStore myCookieStore = new PersistentCookieStore(MainActivity.this);
                    client.setCookieStore(myCookieStore);
                    httpContext = client.getHttpContext();
                    cookies = (CookieStore) httpContext.getAttribute(ClientContext.COOKIE_STORE);
                    if(cookies!=null){
                        for(Cookie c:cookies.getCookies()){
                            System.out.println("main after ~~" + c.getName() + c.getValue());
                        }
                    }else{
                        System.out.println("main  after~~" + "cookies is null");
                    }
    
                    client.get("http://192.168.0.150:8080/test", new AsyncHttpResponseHandler() {
    
                        @Override
                        public void onStart() {
                            // called before request is started
                        }
    
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                            String responseString = new String(response);
                            System.out.println(responseString);
                        }
    
                        @Override
                        public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
                            // called when response HTTP status is "4XX" (eg. 401, 403, 404)
                        }
    
                        @Override
                        public void onRetry(int retryNo) {
                            // called when request is retried
                        }
                    });
                }
            });
        }
    }
  • 相关阅读:
    pspc命令使用(转)
    NSArray数组的使用常用方法(转)
    css兼容的问题 持续更新
    关于memcache
    drupal7 学习笔记(持续更新中...)
    JavaScript的继承 转载
    浏览器的缓存机制
    我的js单例模式
    javascript运行机制
    javascrpt绑定事件之匿名函数
  • 原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/5230172.html
Copyright © 2011-2022 走看看