Я не понимаю, почему onResponse () не вызывается после успешного ответа? - PullRequest
0 голосов
/ 24 марта 2020

Из журнала видно, что веб-сервис правильно возвращает мне JSON. Но по какой-то причине он никогда не входит в метод onResponse .

MainActivity. java

Метод OnResponse () в getaccesstoken () не вызывается после успешного ответа

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

Ответ

D/OkHttp: {"access_token": "IGQVJXWTcyNVBZANmFfeWNSQXUwQmdZAYlUtSHYxc1Q1dUlZAWTVPaTVTZA3dqYU00dC1ocjc4WDJwS2ZA2cVJjMTA3Rkx5QkN4alBlVjFNWjBndkJMcm45ZA0s3dk5HbXBFNnU5empGVGI0WXdrb1ZARZADktVlFjeVJIQUxGRnpv", "user_id": 17841401561947636}

1 Ответ

0 голосов
/ 24 марта 2020

Я думаю, что вы не обрабатываете все случаи правильно, как если бы onFailure вызывал ошибку.

Я не отлаживаю ваш проект, поэтому могу сообщить вам точную ошибку, но я предлагаю вам несколько ссылок, где вы можете найти, как правильно отладить ваш код.

Как отлаживать android приложения

...