GitLap中的Retrofit2和baseurl

时间:2017-10-06 10:35:12

标签: retrofit2 gitlab-api

我试图通过将我的项目的 GitLab 名称显示到recyclerView来学习retrofit2。但我找不到究竟该做什么!!! 根据gitlab wiki我找到了私人令牌和list项目。

这是我的baseUrl,我创建了一个构建器:

Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("https://gitlab.com/api/v4/projects?private_token=xxxxxx")
            .addConverterFactory(GsonConverterFactory.create());

这是我的终点:

@GET("/users/:{userid}/projects")
Call<List<GitLabRepo>> reposForUser(@Path("userid") int user);

我的GitLabRepo pojo类:

class GitLabRepo {

private String name;

public String getName() {
    return name;
}
}

跑完后我得到了这个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sayres.createandroidclient/com.example.sayres.createandroidclient.MainActivity}: java.lang.IllegalArgumentException: baseUrl must end in /: https://gitlab.com/api/v4/projects?private_token=rjezfrSUTn3fcugxLujm/
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:154)
                                                 at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                              Caused by: java.lang.IllegalArgumentException: baseUrl must end in /: https://gitlab.com/api/v4/projects?private_token=xxxxxx/
                                                 at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:513)
                                                 at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:456)
                                                 at com.example.sayres.createandroidclient.MainActivity.onCreate(MainActivity.java:34)
                                                 at android.app.Activity.performCreate(Activity.java:6679)

我知道我的问题与baseUrl有关但是什么是正确的gitLab BaseUrl? 我想在recycelerview列表中将我的项目名称显示在列表中。

******************编辑****************** 我用的是海报:

https://gitlab.com/api/v4/projects?private_token=******/users/:******/projects

我得到了:

{
"message": "401 Unauthorized"
}

我在gitlab wiki看到Status codes并在那里写道:

401 Unauthorized    The user is not authenticated, a valid user token is necessary.

但我添加了私人代币

1 个答案:

答案 0 :(得分:0)

删除正斜杠&#39; /&#39;从得到,改变你的代码

@GET("users/:{userid}/projects/")
Call<List<GitLabRepo>> reposForUser(@Path("userid") int user);

并添加&#39; /&#39;在你的端点像这样

Retrofit.Builder builder = new Retrofit.Builder()
        .baseUrl("https://gitlab.com/api/v4/projects?private_token=xxxxxx/")
        .addConverterFactory(GsonConverterFactory.create());