I create an authentication application, I have 3 methods, the first registers the user, the second gets a client id and secret, and the third gets access and refresh token. Everything works except the third. Let's take a look at the code, I will add only the interface, the retrofit class and the main class. Here is the interface.

public interface SupportopApi { @POST("/api/registration") Call<ResponseBody> registration(@Body SupportopObj supportopObj); @POST("/api/getClientCD") Call<ResponseBody> activate(@Body SupportopObjActivate activate); @GET("/api/token") Call<ResponseBody> getToken(@Query("grant_type") String grant_type, @Query("client_id") String client_id, @Query("client_secret") String client_secret, @Query("email") String email, @Query("password") String password);} 

See I have 3 requests. See 3rd request. Is everything okay Then go ahead. Here is the retrofit class.

 public class ApiClient { private static ApiClient instance; private SupportopApi supportopApi; private ApiClient(String endpoint) { OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() .readTimeout(10, TimeUnit.SECONDS) .connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS); clientBuilder.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); request = request.newBuilder() .addHeader("Content-Type", "application/json") .build(); return chain.proceed(request); } }); supportopApi = new Retrofit.Builder() .baseUrl(endpoint) .client(clientBuilder.build()) .addConverterFactory(GsonConverterFactory.create()) .build() .create(SupportopApi.class); } public static synchronized void initializeInstance(String endpoint) { if (instance == null) { instance = new ApiClient(endpoint); } } public static synchronized ApiClient getInstance() { if (instance == null) { throw new IllegalStateException("PentairAPIClient has not been initialized."); } return instance; } public Call<ResponseBody> registration(SupportopObj supportopObj) { return supportopApi.registration(supportopObj); } public Call<ResponseBody> activation(SupportopObjActivate activate){ return supportopApi.activate(activate); } public Call<ResponseBody> get_token(String grant_type, String client_id, String client_secret, String email, String password){ return supportopApi.getToken(grant_type, client_id, client_secret, email, password); }} 

See the last 3 requests. Everything's Alright? Then go ahead. And here is the main class, I use fragments.

 public class FragmentRegistration extends Fragment { View mainView; EditText username, email, password, name; Button button; ApiClient pentairAPIClient = ApiClient.getInstance(); SupportopObj supportopObj = new SupportopObj(); SupportopObjActivate supportopObjActivate = new SupportopObjActivate(); @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mainView = inflater.inflate (R.layout.registration, container, false); username = mainView.findViewById(R.id.username); email = mainView.findViewById(R.id.email); password = mainView.findViewById(R.id.password); name = mainView.findViewById(R.id.name); button = mainView.findViewById(R.id.register); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { supportopObj.setFirstName("Tester"); supportopObj.setLastName("Testryan"); supportopObj.setUsername(username.getText().toString()); supportopObj.setEmail(email.getText().toString()); supportopObj.setPassword(password.getText().toString()); supportopObjActivate.setUsername(supportopObj.getUsername()); supportopObjActivate.setEmail(supportopObj.getEmail()); supportopObjActivate.setPassword(supportopObj.getPassword()); supportopObjActivate.setType("generic"); updateApp(); } }); return mainView; } public void updateApp() { Call<ResponseBody> call = pentairAPIClient.registration(supportopObj); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { if (response.isSuccessful()) { activationCall(); } else { Toast.makeText(getContext(), "Something went wrong", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Toast.makeText(getContext(), "Error...", Toast.LENGTH_SHORT).show(); } }); } public void activationCall() { Call<ResponseBody> callActive = pentairAPIClient.activation(supportopObjActivate); callActive.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { if (response.isSuccessful()) { try { String data = response.body().string(); JSONObject obj = new JSONObject(data); String client_id = obj.getString("client_id"); String client_secret = obj.getString("client_secret"); tokenCall(client_id, client_secret); } catch (JSONException | IOException e) { e.printStackTrace(); } } else { Toast.makeText(getContext(), "error", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Toast.makeText(getContext(), "Error in activation", Toast.LENGTH_SHORT).show(); } }); } public void tokenCall(String client_id, String client_secret) { Call<ResponseBody> token = pentairAPIClient.get_token("password", client_id, client_secret, supportopObjActivate.getEmail(), supportopObjActivate.getPassword()); token.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { if (response.isSuccessful()) { Toast.makeText(getContext(), String.valueOf(response.body()), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getContext(), "Something wrong.....", Toast.LENGTH_SHORT).show(); } } ----------------------------- LINE 144 @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Toast.makeText(getContext(), "You're on failure", Toast.LENGTH_SHORT).show(); } }); }} 

So you see 3 methods? public void updateApp() registers. public void activationCall() gets clientId and secret, public void tokenCall() gets access and refresh token. The 3rd method does not work. Let's explain. So when I insert breakpoint on Call<ResponseBody> token and token.enqueue(new Callback<ResponseBody>() , I see in debug mode that they get all my arguments as I type (email, password, name ...). But when debugging step by step, after token.enqueue(new Callback<ResponseBody>() it should move to the next line public void onResponse . But after token.enqueue(new Callback<ResponseBody>() it jumps to line 144 (see the fragment class is at the end.) When I comment on the updateApp() method and give arguments directly, it works like this. For example, look at Call<ResponseBody> token = pentairAPIClient.get_token("password", "email@mail.ru", "pass" );

Here is the test server link https://supportop.eu-gb.mybluemix.net/api/token?grant_type=password&client_id={your client id}&client_secret={your client secret}&email={your email}&password=password . As you can see, I have to insert the arguments here (client, email, password ...). I think clearly explained, can you help? Thank.

    0