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

 

你可能感兴趣的:(android下asynchttp库对于session的支持)