改造响应始终为假

时间:2019-10-03 11:43:18

标签: android retrofit

我试图使用Bearer Token来访问API,但是我总是收到错误的响应,响应主体为null。

public interface FreelancerService {
@Headers("Accept: application/json")
@GET("api/freelancers/1")
Call<Model> getData( @Header("Authorization") String authorization);}`

创建翻新实例

    private static final String BASE_URL = "https://match.maffia.ca/";
    public static Retrofit getRetrofitInstance(){
    if (retrofit == null){
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .build();

        retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }
    return retrofit;
}

打来电话...

    FreelancerService service = RetrofitInstance.getRetrofitInstance().create(FreelancerService.class);
    Call<Model> data = service.getData("Bearer " + R.string.token);
    data.enqueue(new Callback<Model>() {
        @Override
        public void onResponse(Call<Model> call, Response<Model> response) {
            Log.d(TAG, "onResponse: " + response.isSuccessful());
        }

        @Override
        public void onFailure(Call<Model> call, Throwable t) {
            Log.d(TAG, "onFailure: " + t.getMessage());
        }
    });

模型类

public class Model {
@SerializedName("data") @Expose
private Data data;
public Data getData() {
    return data;
}
public void setData(Data data) {
    this.data = data;
}}

数据类

public class Data {
@SerializedName("freelancer")@Expose
private Freelancer freelancer;
public Freelancer getFreelancer() {
    return freelancer;
}
public void setFreelancer(Freelancer freelancer) {
    this.freelancer = freelancer;
}}

以同样的方式,我还有更多的课程。

我要获取的JSON。

{
"data": {
    "freelancer": {
        "id": 1,
        "job_title": "Copy & comedy writer",
        "vat": null,
        "resume": null,
        "linkedin": null,
        "has_email_notification": null,
        "has_whatsapp_notification": null,
        "is_snoozing": null,
        "snoozing_until": null,
        "snoozing_from": null,
        "is_member": 0,
        "member_until": null,
        "other_languages": null,
        "location_id": 2,
        "project_type_id": 3,
        "minimum_day_rate_id": 1,
        "experience_id": 5,
        "user_id": 3,
        "created_at": "2019-09-02 10:54:51",
        "updated_at": "2019-09-02 10:54:51",
        "deleted_at": null,
        "competence_values": [
            "Schrijven",
            "rijmen",
            "dichten",
            "cartoons maken en meer."
        ],
        "language_ids": [
            1,
            3
        ],
        "service_ids": [
            13,
            24,
            33,
            34,
            16
        ],
        "availability_ids": [
            4
        ],
        "user": {
            "id": 3,
            "first_name": "Johan Sebastiaan",
            "last_name": "Stuer",
            "email": "johan.sebastiaan.stuer@gmail.com",
            "mobile": "0472809137",
            "has_accepted_general_conditions": 1,
            "has_accepted_maffia_conditions": 0,
            "newsletter_subscriber": 1,
            "type": "freelancer",
            "avatar": "public/avatars/default.png",
            "gender_id": 1,
            "email_verified_at": null,
            "api_token": null,
            "created_at": "2019-09-02 10:54:50",
            "updated_at": "2019-09-02 10:54:50",
            "deleted_at": null
        },
        "languages": [
            {
                "id": 1,
                "name": "Dutch",
                "created_at": "2019-09-02 10:51:44",
                "updated_at": null,
                "deleted_at": null,
                "languages": {
                    "freelancer_id": 1,
                    "language_id": 1,
                    "created_at": "2019-09-02 10:54:51",
                    "updated_at": "2019-09-02 10:54:51"
                }
            },
            {
                "id": 3,
                "name": "English",
                "created_at": "2019-09-02 10:51:44",
                "updated_at": null,
                "deleted_at": null,
                "languages": {
                    "freelancer_id": 1,
                    "language_id": 3,
                    "created_at": "2019-09-02 10:54:51",
                    "updated_at": "2019-09-02 10:54:51"
                }
            }
        ],
        "services": [
            {
                "id": 13,
                "name": "Copy writing",
                "created_at": "2019-09-02 10:51:43",
                "updated_at": null,
                "deleted_at": null,
                "services": {
                    "freelancer_id": 1,
                    "service_id": 13,
                    "created_at": "2019-09-02 10:54:51",
                    "updated_at": "2019-09-02 10:54:51"
                }
            },
            {
                "id": 24,
                "name": "Illustration",
                "created_at": "2019-09-02 10:51:43",
                "updated_at": null,
                "deleted_at": null,
                "services": {
                    "freelancer_id": 1,
                    "service_id": 24,
                    "created_at": "2019-09-02 10:54:51",
                    "updated_at": "2019-09-02 10:54:51"
                }
            },
            {
                "id": 33,
                "name": "Social media",
                "created_at": "2019-09-02 10:51:44",
                "updated_at": null,
                "deleted_at": null,
                "services": {
                    "freelancer_id": 1,
                    "service_id": 33,
                    "created_at": "2019-09-02 10:54:51",
                    "updated_at": "2019-09-02 10:54:51"
                }
            },
            {
                "id": 34,
                "name": "Translation",
                "created_at": "2019-09-02 10:51:44",
                "updated_at": null,
                "deleted_at": null,
                "services": {
                    "freelancer_id": 1,
                    "service_id": 34,
                    "created_at": "2019-09-02 10:54:51",
                    "updated_at": "2019-09-02 10:54:51"
                }
            },
            {
                "id": 16,
                "name": "Desktop publishing",
                "created_at": "2019-09-02 10:51:43",
                "updated_at": null,
                "deleted_at": null,
                "services": {
                    "freelancer_id": 1,
                    "service_id": 16,
                    "created_at": "2019-09-02 10:54:51",
                    "updated_at": "2019-09-02 10:54:51"
                }
            }
        ],
        "competences": [
            {
                "id": 1,
                "name": "Schrijven",
                "created_at": "2019-09-02 10:54:51",
                "updated_at": "2019-09-02 10:54:51",
                "deleted_at": null,
                "competences": {
                    "freelancer_id": 1,
                    "competence_id": 1,
                    "created_at": "2019-09-02 10:54:51",
                    "updated_at": "2019-09-02 10:54:51"
                }
            },
            {
                "id": 2,
                "name": "rijmen",
                "created_at": "2019-09-02 10:54:51",
                "updated_at": "2019-09-02 10:54:51",
                "deleted_at": null,
                "competences": {
                    "freelancer_id": 1,
                    "competence_id": 2,
                    "created_at": "2019-09-02 10:54:51",
                    "updated_at": "2019-09-02 10:54:51"
                }
            },
            {
                "id": 3,
                "name": "dichten",
                "created_at": "2019-09-02 10:54:51",
                "updated_at": "2019-09-02 10:54:51",
                "deleted_at": null,
                "competences": {
                    "freelancer_id": 1,
                    "competence_id": 3,
                    "created_at": "2019-09-02 10:54:51",
                    "updated_at": "2019-09-02 10:54:51"
                }
            },
            {
                "id": 4,
                "name": "cartoons maken en meer.",
                "created_at": "2019-09-02 10:54:51",
                "updated_at": "2019-09-02 10:54:51",
                "deleted_at": null,
                "competences": {
                    "freelancer_id": 1,
                    "competence_id": 4,
                    "created_at": "2019-09-02 10:54:51",
                    "updated_at": "2019-09-02 10:54:51"
                }
            }
        ],
        "availabilities": [
            {
                "id": 4,
                "option": "On offer",
                "created_at": "2019-09-02 10:51:44",
                "updated_at": null,
                "deleted_at": null,
                "freelancer_availabilities": {
                    "freelancer_id": 1,
                    "availability_id": 4,
                    "created_at": "2019-09-02 10:54:51",
                    "updated_at": "2019-09-02 10:54:51"
                }
            }
        ]
    }
}}

我不明白问题出在哪里。当我在Postman中使用授权将“ Bearer Token”作为API时,得到的结果。

1 个答案:

答案 0 :(得分:0)

我认为问题出在您的改造实例中。可能是timeout connection

  

只需看看我的Retrofit类:

public class RetrofitSingleton {

    public static final String BASE_URL = "https://match.maffia.ca/";
    private static Retrofit retrofit = null;
    private static Context sContext;
    private static Retrofit sRetrofit;

    public RetrofitSingleton() {
    }

    public synchronized static Retrofit getInstance(Context context) {
        sContext=context;
        if(sRetrofit==null){
            createRetrofit(context);
        }
        return sRetrofit;
    }

    private static void createRetrofit(Context context){


                OkHttpClient okHttpClient= null;
                okHttpClient = new OkHttpClient.Builder()

                .connectTimeout(2, TimeUnit.MINUTES)
                .readTimeout(2, TimeUnit.MINUTES)
                .writeTimeout(2, TimeUnit.MINUTES)
                .addInterceptor(new ConnectivityInterceptor(context))
                .retryOnConnectionFailure(false)
                .cache(null)//new Cache(sContext.getCacheDir(),10*1024*1024)
                .build();

        sRetrofit= new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
}

希望对您有帮助。