You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.9 KiB
Java

package com.common.commonlibtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.common.commonlib.net.RequestCallBack;
import com.common.commonlib.net.interceptor.RequestHeadInterceptor;
import com.common.commonlib.net.interceptor.ResponseHeadInterceptor;
import com.common.commonlibtest.bean.ArticlesResponse;
import com.common.commonlibtest.bean.CollectionResponse;
import com.common.commonlibtest.bean.LoginResponse;
import com.common.commonlibtest.manager.CollectionLoader;
import com.common.commonlibtest.manager.InternetLoader;
import com.common.commonlibtest.manager.LoginLoader;
import com.yinuo.commonlibtest.databinding.ActivityMainBinding;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Headers;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
initView();
}
private void initView() {
binding.tv.setOnClickListener(l -> {
InternetLoader loader = new InternetLoader();
loader.getHomeArticles(0, new RequestCallBack<ArticlesResponse>() {
@Override
public void onResult(ArticlesResponse result) {
Log.d(TAG, result.getData().toString());
}
@Override
public void onError(@Nullable String error) {
Log.d(TAG, "onError");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
});
});
binding.btn.setOnClickListener(l -> {
List<RequestHeadInterceptor> interceptors = new ArrayList<>();
RequestHeadInterceptor.Builder builder = new RequestHeadInterceptor.Builder();
builder.addHeadParams("Cookie", "loginUserName_wanandroid_com=wang11;token_pass_wanandroid_com=6fbec4fe15d69af8b4531171798f3926;loginUserName=wang11;token_pass=6fbec4fe15d69af8b4531171798f3926");
interceptors.add(builder.build());
CollectionLoader loader = new CollectionLoader(interceptors);
loader.getCollections(0, new RequestCallBack<CollectionResponse>() {
@Override
public void onResult(CollectionResponse result) {
Log.d(TAG, "onResult");
}
@Override
public void onError(@Nullable String error) {
Log.d(TAG, "onError");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
});
});
binding.loginBtn.setOnClickListener(l -> {
List<ResponseHeadInterceptor> interceptors = new ArrayList<>();
interceptors.add(new ResponseHeadInterceptor(response -> {
Headers headers = response.headers();
List<String> cookies = headers.values("Set-Cookie");
}));
new LoginLoader(interceptors).login("wang11", "wang456852", new RequestCallBack<LoginResponse>() {
@Override
public void onResult(LoginResponse result) {
Log.d(TAG, "onResult");
}
@Override
public void onError(@Nullable String error) {
Log.d(TAG, "onError");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
});
});
}
}