0

From the log I can see that the web service correctly returns the JSON to me. But for some reason it never enters the onResponse method.

MainActivity.java

OnResponse() method in getaccesstoken() not triggered after successful response

package com.example.alexandra.instagramlogin;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.alexandra.instagramlogin.models.AuthToken;
import com.example.alexandra.instagramlogin.rest.RestClient;
import com.example.alexandra.instagramlogin.rest.services.Auth;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity
        implements AuthenticationListener {

    private static final String TAG = "MainActivity";
    private String token = null;
    private String code = null;
    private AppPreferences appPreferences = null;
    private AuthenticationDialog authenticationDialog = null;
    private Button button = null;
    private View info = null;
    AuthToken authToken = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.btn_login);
        info = findViewById(R.id.info);
        appPreferences = new AppPreferences(this);

        //check already have access token
    }


    public void login() {
        button.setText("LOGOUT");
        info.setVisibility(View.VISIBLE);
        ImageView pic = findViewById(R.id.pic);
        Picasso.with(this).load(appPreferences.getString(AppPreferences.PROFILE_PIC)).into(pic);
        TextView id = findViewById(R.id.id);
        id.setText(appPreferences.getString(AppPreferences.USER_ID));
        TextView name = findViewById(R.id.name);
        name.setText(appPreferences.getString(AppPreferences.USER_NAME));
    }

    public void logout() {
        button.setText("INSTAGRAM LOGIN");
        token = null;
        info.setVisibility(View.GONE);
        appPreferences.clear();
    }

    @Override
    public void onTokenReceived(String auth_code) {
        Log.d(TAG, auth_code);
        appPreferences.putString(AppPreferences.CODE, auth_code);
        code = auth_code;

        getAccessToken(code);
    }

    //getting access token
    private void getAccessToken(final String code) {
        Auth auth = RestClient.getInstance();

        Call<AuthToken>authTokenCall = auth.getAuthToken(getString(R.string.client_id),getString(R.string.client_secret),"authorization_code",getString(R.string.redirect_url),
                code);


        authTokenCall.enqueue(new Callback<AuthToken>() {
            @Override
            public void onResponse(Call<AuthToken> call, Response<AuthToken> response) {
                Log.d(TAG, "onResponse: ");
            }

            @Override
            public void onFailure(Call<AuthToken> call, Throwable t) {

            }
        });



    }

    public void onClick(View view) {
        if(token!=null)
        {
            logout();
        }
        else {
            authenticationDialog = new AuthenticationDialog(this, this);
            authenticationDialog.setCancelable(true);
            authenticationDialog.show();
        }
    }



}

pojo(AuthToken)

package com.example.alexandra.instagramlogin.models;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class AuthToken {

    @SerializedName("access_token")
    @Expose
    private String access_token;

    @SerializedName("user_id")
    @Expose
    private Integer user_id;

    public String getAccessToken() {
        return access_token;
    }

    public void setAccessToken(String accessToken) {
        this.access_token = accessToken;
    }

    public Integer getUserId() {
        return user_id;
    }

    public void setUserId(Integer userId) {
        this.user_id = userId;
    }

}

Auth.java

package com.example.alexandra.instagramlogin.rest.services;

import com.example.alexandra.instagramlogin.Classes.AuthorizationToken;
import com.example.alexandra.instagramlogin.models.AuthToken;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface Auth {

    @FormUrlEncoded
    @POST("oauth/access_token")
    Call<AuthToken>getAuthToken(@Field("client_id")String clientid,@Field("client_secret")String client_secret,@Field("grant_type")String grant_type
    ,@Field("redirect_uri")String redirect_uri,@Field("code")String code);
}

Response

D/OkHttp: {"access_token": "IGQVJXWTcyNVBZANmFfeWNSQXUwQmdZAYlUtSHYxc1Q1dUlZAWTVPaTVTZA3dqYU00dC1ocjc4WDJwS2ZA2cVJjMTA3Rkx5QkN4alBlVjFNWjBndkJMcm45ZA0s3dk5HbXBFNnU5empGVGI0WXdrb1ZARZADktVlFjeVJIQUxGRnpv", "user_id": 17841401561947636}
halfer
  • 19,824
  • 17
  • 99
  • 186
Karan Parwani
  • 21
  • 2
  • 2

1 Answers1

0

I think you are not handling all case correctly like what if onFailure trigger for error.

I am not debugging you project so exact error i can tell you but i suggest you some links where you can find how to debug your code properly.

How to debug android apps

Pratyush
  • 401
  • 3
  • 11