I am using retrofit library for parsing data from json.But now i required to pass token in my request. I have set token in one Global class.Now i want to use that token in APIClient.But when i pass , it through null.
This is my APIClient
public class APIClient {
public static final String BASE_URL = "http://kartpays.bizs/api/v5/";
private static Retrofit retrofit = null;
public static final String FOLLOW_URL ="http://kartpays.biz/api/v1/follow/";
public static Retrofit getRetrofitInstance() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request originalRequest = chain.request();
Request request = originalRequest.newBuilder()
.header("token", **I want to pass token here**)
.header("Content-Type", "application/json")
.method(originalRequest.method(), originalRequest.body())
.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
retrofit = new Retrofit.Builder()
.baseUrl(FOLLOW_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
In this class i stored token
public class LibFile {
Context context;
public static SharedPreferences settings;
private static LibFile instance;
public static LibFile getInstance(Context context) {
if (instance == null) {
instance = new LibFile(context);
}
return instance;
}
public LibFile(Context context) {
this.context = context;
settings = context.getSharedPreferences(AppConstants.PREFS_FILE_NAME_PARAM, 0);
}
public String getUser_id() {
return settings.getString("user_id", "");
}
public void setUser_id(String link) {
settings.edit().putString("user_id", link).commit(); //get link from here
}
public String getToken() {
return settings.getString("token","");//pass key here
}
public static void setToken(String userName) {
settings.edit().putString("token", userName).commit();//get key from here
}
public void clearCache() {
settings.edit().clear().commit();
settings.edit().remove("link").commit();
}
}
Please Suggest me how to pass token dynamically. I could not pass static token because it changes continuously.Thanks in advance
If your token is stored in SharedPreference then check out below changes of your code,
public static Retrofit getRetrofitInstance(Context mContext) {
SharedPreferences settings = mContext.getSharedPreferences(AppConstants.PREFS_FILE_NAME_PARAM, 0);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request originalRequest = chain.request();
Request request = originalRequest.newBuilder()
.header("token", settings.getString("token", ""))
.header("Content-Type", "application/json")
.method(originalRequest.method(), originalRequest.body())
.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
retrofit = new Retrofit.Builder()
.baseUrl(FOLLOW_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
A better solution suiting your code would be:
Request request = originalRequest.newBuilder()
.header("token", LibFile.getInstance(mContext).getToken())
.header("Content-Type", "application/json")
.method(originalRequest.method(), originalRequest.body())
.build();
Check this line: .header("token", LibFile.getInstance(mContext).getToken())
Which take an instance from the LibFile and gets a token.
In your http request, the header key should be "Authorization" instead of "token".
Request request = originalRequest.newBuilder()
.header("Authorization", **I want to pass token here**)
.header("Content-Type", "application/json")
.method(originalRequest.method(), originalRequest.body())
.build();
You can use below methods to put and get token from shared preferences.
public static boolean setStringPreference(String key, String value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null && !TextUtils.isEmpty(key)) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
return editor.commit();
}
return false;
}
public static String getStringPreference(String key) {
String value = null;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
value = preferences.getString(key, null);
}
return value;
}
Related
I have an android studio application that connects to a nodejs backend server with user authentication. I can log in and register from my app but it does not store a session. So I can not get session based functionality yet.
I need to add functionality to store a session. For this how do I do this with the retrofit interface.
I want to log in start a session so I can have user logged in access to other routes on the server.
Or is there another interface for android studio that will allow for cookies and sessions?
Retrofit interface
public interface RetrofitInterface {
#POST("/login")
Call<Login_result> executeLogin(#Body HashMap<String, String> map);
#POST("/signup")
Call<Void> executeSignup(#Body HashMap<String, String>map);
#POST("/add_data")
Call<Void> executeAdd_data(#Body HashMap<String, String>map);
#POST("/logout")
Call<Void> executeLogout(#Body HashMap<String, String>map);
#GET("/test")
Call<Void> executeTest();
}
**Main code**
```java
/*Updated this*/
Context context = this;
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(new OkhttpClient.builder()
.addInterceptor(new ReceivedCookiesInterceptor(context)
.addInterceptor(new AddCookiesInterceptor(context)
).build())
.addConverterFactory(GsonConverterFactory.create())
.build();
retrofitInterface = retrofit.create(RetrofitInterface.class);
Log in code
HashMap<String,String> map = new HashMap<>();
//map.put("email",emailEdit.getText().toString());//
map.put("username", usernameEdit.getText().toString());
map.put("password", passwordEdit.getText().toString());
Call<Login_result> call =
retrofitInterface.executeLogin(map);//Run the post
call.enqueue(new Callback<Login_result>()
{
#Override
public void onResponse(Call<Login_result> call, Response<Login_result> response) {
if(response.code() == 200)
{
/*Login_result result = response.body();
AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
builder1.setTitle(result.getUsernname());
builder1.setMessage(result.getEmail());
builder1.show();*/
Toast.makeText(MainActivity.this, "Logged in", Toast.LENGTH_SHORT).show();
}else if(response.code() == 404)
{
Toast.makeText(MainActivity.this, "Incorrect usernanme or password", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(Call<Login_result> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getMessage(),Toast.LENGTH_LONG).show();
}
});
You would need to create an two interceptors and store the cookie information in Shared Preferences
public class ReceivedCookiesInterceptor implements Interceptor {
private Context context;
public ReceivedCookiesInterceptor(Context context) {
this.context = context;
}
#Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
if (!originalResponse.headers("Set-Cookie").isEmpty()) {
HashSet<String> cookies = (HashSet<String>) PreferenceManager.getDefaultSharedPreferences(context).getStringSet("PREF_COOKIES", new HashSet<String>());
for (String header : originalResponse.headers("Set-Cookie")) {
cookies.add(header);
}
SharedPreferences.Editor memes = PreferenceManager.getDefaultSharedPreferences(context).edit();
memes.putStringSet("PREF_COOKIES", cookies).apply();
memes.commit();
}
return originalResponse;
}
}
And then reverse to add cookies to the outgoing request
public class AddCookiesInterceptor implements Interceptor {
public static final String PREF_COOKIES = "PREF_COOKIES";
private Context context;
public AddCookiesInterceptor(Context context) {
this.context = context;
}
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
HashSet<String> preferences = (HashSet<String>) PreferenceManager.getDefaultSharedPreferences(context).getStringSet(PREF_COOKIES, new HashSet<String>());
Request original = chain.request();
if(original.url().toString().contains("distributor")){
for (String cookie : preferences) {
builder.addHeader("Cookie", cookie);
}
}
return chain.proceed(builder.build());
}
}
Which then you would need to change your Retrofit instance to the below
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(new OkhttpClient.builder()
.addInterceptor(new ReceivedCookiesInterceptor(context)
.addInterceptor(new AddCookiesInterceptor(context)
).build())
.addConverterFactory(GsonConverterFactory.create())
.build();
retrofitInterface = retrofit.create(RetrofitInterface.class);
I have a class to handle token refreshes once they expire. The code is below:
public class TokenAuthenticator implements Authenticator {
#Nullable
#Override
public synchronized Request authenticate(#NonNull Route route, #NonNull Response response) throws IOException {
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<User> call = apiInterface.refreshTokens(new ClientRequest(Songa.getContext().getString(R.string.client_id),
App.getContext().getString(R.string.client_secret),
App.getContext().getString(R.string.grant_type), getRAGUser().getRefreshToken()));
User ragUser = call.execute().body();
if (ragUser != null) {
Gson gson = new Gson();
String user = gson.toJson(ragUser);
PrefUtils.putString(Constants.USER, user);
long tokenExpiryPeriod = System.currentTimeMillis() + Long.parseLong(ragUser.getExpiryPeriod());
PrefUtils.putLong(Constants.TOKEN_EXPIRY_PERIOD, tokenExpiryPeriod);
return response.request().newBuilder().header("Authorization", "Bearer " + ragUser.getAccessToken()).build();
} else {
if (responseCount(response) >= 3) {
Log.e("TokenAuthenticator", String.valueOf(responseCount(response)));
//we have failed 3 times; log the user out
EventBus.getDefault().post(new LogoutEvent());
return null;
}
}
return null;
}
private int responseCount(Response response) {
int result = 1;
while ((response = response.priorResponse()) != null) {
result++;
}
return result;
}
}
My intention is that once a token expires, the authenticator should retry a maximum of three times before giving up and logging out the user. However, the code below executes each request three times, even with a valid token.
I've always assumed that the Authenticator class only steps in when the token expires but from my logs, I can see that it is called every time a new request is made.
The following is the code from my Retrofit client:
public class RestClient {
private static final String BASE_URL = "https://my.base.url/api/v3/";
private static String token = "Bearer " + getAccessToken();
private static Retrofit retrofit = null;
public RestClient() {
}
public static Retrofit getClient() {
if (retrofit == null) {
TokenAuthenticator tokenAuthenticator = new TokenAuthenticator();
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(1);
Gson gson = new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
#Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class);
}
#Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}).create();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okClient = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.authenticator(tokenAuthenticator)
.addInterceptor(loggingInterceptor)
.addInterceptor(chain -> {
Request original = chain.request();
Request request = original.newBuilder()
.addHeader("Authorization", token)
.addHeader("Content-Type", "application/json")
.build();
return chain.proceed(request);
})
.addInterceptor(loggingInterceptor)
.dispatcher(dispatcher)
.build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okClient)
.build();
}
return retrofit;
}
}
Is there a better way of implementing token authentication with my requirements; 3 retries before logout?
I am trying to add basic authentication (username and password) to a Retrofit OkHttp client. This is the code I have so far:
private static Retrofit createMMSATService(String baseUrl, String user, String pass) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
I am using Retrofit 2.2 and this tutorial suggests using AuthenticationInterceptor, but this class is not available.
Where is the correct place to add the credentials? Do I have to add them to my interceptor, client or Retrofit object? And how do I do that?
Find the Solution
1.Write a Interceptor class
import java.io.IOException;
import okhttp3.Credentials;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
public class BasicAuthInterceptor implements Interceptor {
private String credentials;
public BasicAuthInterceptor(String user, String password) {
this.credentials = Credentials.basic(user, password);
}
#Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request authenticatedRequest = request.newBuilder()
.header("Authorization", credentials).build();
return chain.proceed(authenticatedRequest);
}
}
2.Finally, add the interceptor to an OkHttp client
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new BasicAuthInterceptor(username, password))
.build();
Retrofit 2
public class ServiceGenerator {
public static final String API_BASE_URL = "https://your.api-base.url";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
private static Retrofit retrofit = builder.build();
public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null, null);
}
public static <S> S createService(
Class<S> serviceClass, String username, String password) {
if (!TextUtils.isEmpty(username)
&& !TextUtils.isEmpty(password)) {
String authToken = Credentials.basic(username, password);
return createService(serviceClass, authToken);
}
return createService(serviceClass, null);
}
public static <S> S createService(
Class<S> serviceClass, final String authToken) {
if (!TextUtils.isEmpty(authToken)) {
AuthenticationInterceptor interceptor =
new AuthenticationInterceptor(authToken);
if (!httpClient.interceptors().contains(interceptor)) {
httpClient.addInterceptor(interceptor);
builder.client(httpClient.build());
retrofit = builder.build();
}
}
return retrofit.create(serviceClass);
}
}
Retrofit 1.9
public class ServiceGenerator {
public static final String API_BASE_URL = "https://your.api-base.url";
private static RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint(API_BASE_URL)
.setClient(new OkClient(new OkHttpClient()));
public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null, null);
}
public static <S> S createService(Class<S> serviceClass, String username, String password) {
if (username != null && password != null) {
// concatenate username and password with colon for authentication
String credentials = username + ":" + password;
// create Base64 encodet string
final String basic =
"Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
builder.setRequestInterceptor(new RequestInterceptor() {
#Override
public void intercept(RequestFacade request) {
request.addHeader("Authorization", basic);
request.addHeader("Accept", "application/json");
}
});
}
RestAdapter adapter = builder.build();
return adapter.create(serviceClass);
}
}
AuthenticationInterceptor.java
public class AuthenticationInterceptor implements Interceptor {
private String authToken;
public AuthenticationInterceptor(String token) {
this.authToken = token;
}
#Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder builder = original.newBuilder()
.header("Authorization", authToken);
Request request = builder.build();
return chain.proceed(request);
}
}
Usage
Retrofit 2
Interface
public interface LoginService {
#POST("/login")
Call<User> basicLogin();
}
Requester
LoginService loginService =
ServiceGenerator.createService(LoginService.class, "user", "secretpassword");
Call<User> call = loginService.basicLogin();
call.enqueue(new Callback<User >() {
#Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
// user object available
} else {
// error response, no access to resource?
}
}
#Override
public void onFailure(Call<User> call, Throwable t) {
// something went completely south (like no internet connection)
Log.d("Error", t.getMessage());
}
}
Retrofit 1.9
Interface
public interface LoginService {
#POST("/login")
void basicLogin(Callback<User> cb);
}
Requester
LoginService loginService =
ServiceGenerator.createService(LoginService.class, "user", "secretpassword");
loginService.basicLogin(new Callback<User>() {
#Override
public void success(User user, Response response) {
// user object available
}
#Override
public void failure(RetrofitError error) {
// handle errors, too
}
});
More information see here.
add header interceptor
public class HeaderInterceptor implements Interceptor {
private PreferencesRepository mPrefs;
private String mAuth;
public HeaderInterceptor(PreferencesRepository p) {
mPrefs = p;
}
#Override
public Response intercept(Chain chain) throws IOException {
mAuth = (mPrefs.getAuthToken() != null)?mPrefs.getAuthToken():"";
Request r = chain.request()
.newBuilder()
.addHeader("Accept", "application/json")
// authorization token here
.addHeader("Authorization", "Bearer" + mAuth)
.build();
return chain.proceed(r);
}
}
add cacheinterceptor (optional)
public class CacheInterceptor implements Interceptor {
Context mContext;
public CacheInterceptor(Context context) {
this.mContext = context;
}
#Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (request.method().equals("GET")) {
if (DeviceUtils.isConnected(mContext)) {
request = request.newBuilder()
.header(Constant.CACHE_CONTROL, "only-if-cached")
.build();
} else {
request = request.newBuilder()
.header(Constant.CACHE_CONTROL, "public, max-stale=2419200")
.build();
}
}
Response originalResponse = chain.proceed(request);
return originalResponse.newBuilder()
.header(Constant.CACHE_CONTROL, "max-age=600")
.build();
}
}
implement it
HttpLoggingInterceptor logger = new HttpLoggingInterceptor();
logger.setLevel(HttpLoggingInterceptor.Level.BODY);
long SIZE_OF_CACHE = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(new File(mContext.getCacheDir(), "http"), SIZE_OF_CACHE);
new OkHttpClient.Builder()
.addInterceptor(logger)
.addInterceptor(new HeaderInterceptor(u))
.cache(cache)
.addNetworkInterceptor(new CacheInterceptor(mContext))
.connectTimeout(Constant.CONNECTTIMEOUT, TimeUnit.SECONDS)
.readTimeout(Constant.READTIMEOUT, TimeUnit.SECONDS)
.writeTimeout(Constant.WRITETIMEOUT, TimeUnit.SECONDS)
.build();
Of course using auth interceptor is correct way (as explained in other answers). Although, if you need basic authentication only for single call, then auth header can be added directly in Retrofit request:
import okhttp3.Credentials
// Create credentials
val login = "some login"
val password = "some password"
// Below code will create correct Base64 encoded Basic Auth credentials
val credentials = Credentials.basic(login, password)
// Then in your Retrofit API interface
interface MyApi {
#POST("get_user")
fun getUser(#Header("Authorization") credentials: String): ResponseBody
}
Well, I'm having problems to connect with my https URL, all works with postman, but I canĀ“t do it with my Android App, Can someone help me?
Image of code.
public class NetworkUtil{
public static RetrofitInterface getRetrofit(){
return new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build().create(RetrofitInterface.class);
}
public static RetrofitInterface getRetrofit(String email, String password) {
String credentials = email + ":" + password;
String basic = "Basic " + Base64.encodeToString(credentials.getBytes(),Base64.NO_WRAP);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder builder = original.newBuilder()
.addHeader("Authorization", basic)
.method(original.method(),original.body());
return chain.proceed(builder.build());
});
return new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.client(httpClient.build())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build().create(RetrofitInterface.class);
}
I need to authenticate the tokens, how can i add that to the okhttp?
public static RetrofitInterface getRetrofit(String token) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder builder = original.newBuilder()
.addHeader("x-access-token", token)
.method(original.method(),original.body());
return chain.proceed(builder.build());
});
return new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.client(okHttpClient)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build().create(RetrofitInterface.class);
}
}
Thanks in advance!
Try to use this code HTTPS working with OkHttp3 and Retrofit2
create API interface
public interface API {
#GET("/users/{user}")
Observable<Result<User>> getUser(#Path("user") String user);
}
create apiManager class which contains OKHttp3 and retrofit2 initialization and base url
public class ApiManager {
Context context;
public static final String BASE_URL = "https://api.github.com/";
private OkHttpClient okHttpClient;
private Authenticator authenticator = new Authenticator() {
#Override
public Request authenticate(Route route, Response response) {
return null;
}
};
private ApiManager() {
}
public void setAuthenticator(Authenticator authenticator) {
this.authenticator = authenticator;
}
public static class Builder {
String email, password;
ApiManager apiManager = new ApiManager();
public Builder setAuthenticator(Authenticator authenticator) {
apiManager.setAuthenticator(authenticator);
return this;
}
public ApiManager build(String param_email, String param_password) {
this.email = param_email;
this.password = param_password;
return apiManager.newInstance(email, password);
}
}
public class RequestTokenInterceptor implements Interceptor {
String email, password;
String credentials, basic;
public RequestTokenInterceptor(String email, String password) {
this.email = email;
this.password = password;
credentials = email + ":" + password;
basic = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
}
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request.Builder builder = original.newBuilder()
.addHeader("Authorization", basic)
.method(original.method(), original.body());
return chain.proceed(builder.build());
}
}
private ApiManager newInstance(String email, String password) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
#Override
public void log(String message) {
Log.i("http", message);
}
});
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(logging)
.addInterceptor(new RequestTokenInterceptor(email, password))
.authenticator(authenticator)
.build();
return this;
}
public <T> T createRest(Class<T> t) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(okHttpClient)
.build();
return retrofit.create(t);
}
}
in Gradle add the libraries
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp-ws:3.4.1'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
In MainActivity add
public class MainActivity extends AppCompatActivity {
private ProgressDialog feedbackDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feedbackDialog = new ProgressDialog(this);
feedbackDialog.setCanceledOnTouchOutside(false);
getUser();
}
public void getUser() {
API accountAPI4 = createRestApi4();
if (accountAPI4 != null) {
showFeedback(getResources().getString(R.string.loading));
BackgroundThreadObservable.toBackground(accountAPI4.getUser("SafAmin"))
.subscribe(new Action1<Result<User>>() {
#Override
public void call(Result<User> user) {
dismissFeedback();
Log.e("GIT_HUB","Github Name :"+user.response().body().getName()+"\nWebsite :"+user.response().body().getBlog());
}
}, new Action1<Throwable>() {
#Override
public void call(Throwable throwable) {
// do something with the error
if (throwable != null) {
dismissFeedback();
}
}
}
);
} else {
dismissFeedback();
}
}
public static API createRestApi4() {
ApiManager apiManager = new ApiManager.Builder().build(email, password);
return apiManager.createRest(API.class);
}
public void showFeedback(String message) {
feedbackDialog.setMessage(message);
feedbackDialog.show();
}
public void dismissFeedback() {
feedbackDialog.dismiss();
}
}
in AndroidManifest add
<uses-permission android:name="android.permission.INTERNET"/>
Hope it helps.
I'm making service that makes user able to login in LoginActivity, and if login is successful, user can post something in PostActivity. I'm using Restful api.
I found good sharedpreference example on github : https://gist.github.com/nikhiljha/52d45ca69a8415c6990d2a63f61184ff
and
https://gist.github.com/tsuharesu/cbfd8f02d46498b01f1b
AddCookiesInterceptor.java
public class AddCookiesInterceptor implements Interceptor {
public static final String PREF_COOKIES = "PREF_COOKIES";
private Context context;
public AddCookiesInterceptor(Context context) {
this.context = context;
}
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
HashSet<String> preferences = (HashSet<String>) PreferenceManager.getDefaultSharedPreferences(context).getStringSet(PREF_COOKIES, new HashSet<String>());
for (String cookie : preferences) {
builder.addHeader("Cookie", cookie);
}
return chain.proceed(builder.build());
}
}
RecievedCookiesInterceptor.java
public class ReceivedCookiesInterceptor implements Interceptor {
private Context context;
public ReceivedCookiesInterceptor(Context context) {
this.context = context;
}
#Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
if (!originalResponse.headers("Set-Cookie").isEmpty()) {
HashSet<String> cookies = (HashSet<String>) PreferenceManager.getDefaultSharedPreferences(context).getStringSet("PREF_COOKIES", new HashSet<String>());
for (String header : originalResponse.headers("Set-Cookie")) {
cookies.add(header);
}
SharedPreferences.Editor memes = PreferenceManager.getDefaultSharedPreferences(context).edit();
memes.putStringSet("PREF_COOKIES", cookies).apply();
memes.commit();
}
return originalResponse;
}
}
I used this code very well on my LoginActivity like this,
OkHttpClient client = new OkHttpClient();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.interceptors().add(new AddCookiesInterceptor(getApplicationContext()));
builder.interceptors().add(new RecievedCookiesInterceptor(getApplicationContext()));
client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(LoginApiService.Login_API_URL)
.build();
loginApiService = retrofit.create(LoginApiService.class);
Call<ResponseBody> getkey = loginApiService.getkey(loginData);
getkey.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if(response.code() == 200) {
startActivity(new Intent(LoginActivity.this, PostActivity.class));
}
}
But in PostActivity, (it's from if (response.code() == 200) startActivity)
I used this like
OkHttpClient client = new OkHttpClient();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.interceptors().add(new AddCookiesInterceptor(getApplicationContext()));
#just used AddCookiesInterceptor, Not RecievedCookiesInterceptor
client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(PostApiService.Post_API_URL)
.build();
PostApiService = retrofit.create(PostApiService.class);
It give me HTTP/1.1 401 error, I found it means there is problem in Headers.
I write PostApiService like this
public interface PostApiService {
#POST("posts/")
Call<ResponseBody> gettest(#Body TextData textData);}
My question :
I want Header like
key : Authorization //
value : Token e0af91707f0434a1a2a7581dd3f4f483bdd717
Where do i modify?
I know too much codes bothers you maybe, so I have to say i'm very sorry.