使用泛型调用API进行改造,调用结果onFailure()

时间:2018-06-13 18:30:48

标签: java android api generics retrofit

我正在尝试使用泛型来调用API,但不会显示它们,也无法检索它们。但是,如果我在MainActivity类中完成所有“脏”工作,它可以正常工作吗?我确实在它破裂之前就收到了一个奇怪的错误。

MainActivity.java

listView = (ListView) findViewById(R.id.pagination_list);

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("https://api.github.com/")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();

    GitHubClient c = ServiceGenerator.createService(GitHubClient.class);

    GitHubClient client = retrofit.create(GitHubClient.class);
     //Call<List<GitHubRepo>> call = client.reposForUser("fs-opensource");
    // Call<List<GitHubRepo>> call = client.reposForUser("Ricardo96MC");
    //Call<List<GitHubRepo>> call = c.reposForUser("Ricardo96MC");
   Call<List<GitHubRepo>> call = c.reposForUser("fs-opensource");


    call.enqueue(new Callback<List<GitHubRepo>>() {
        @Override
        public void onResponse(Call<List<GitHubRepo>> call, Response<List<GitHubRepo>> response) {
             List<GitHubRepo> repos = response.body();

            Log.e("Repos found"," Should be displaying on device");
            listView.setAdapter(new GitHubRepoAdapter(MainActivity.this, repos));
        }

        @Override
        public void onFailure(Call<List<GitHubRepo>> call, Throwable t) {
            Log.e("onFailure", "WHYYYYYYY");
            Toast.makeText(MainActivity.this, "error: ", Toast.LENGTH_LONG).show();
        }
    });
}

使用GitHub客户端取得了成功,但...... ServiceGenerator.java

 private static final String BASE_URL = "https://api/github.com/";

//Make static so theres one instance for entire app
private static Retrofit.Builder builder =  new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create());

//One instance for entire app
public static Retrofit retrofit = builder.build();

//Uses generics to make API call dynamic
public static<T>T createService(Class<T> serviceClass){

    Log.e("createService() : "," service created?");
    return retrofit.create(serviceClass);
}

导致空白UI和以下日志..

06-13 18:17:07.316 16471-16471/? E/libprocessgroup: failed to make and chown /acct/uid_10059: Read-only file system
06-13 18:17:07.317 16471-16471/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
06-13 18:17:07.317 16471-16471/? I/art: Not late-enabling -Xcheck:jni (already on)
06-13 18:17:07.436 16471-16471/com.example.ricardo.retrofittut I/InstantRun: starting instant run server: is main process
06-13 18:17:07.489 16471-16471/com.example.ricardo.retrofittut W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-13 18:17:07.665 16471-16471/com.example.ricardo.retrofittut E/createService() ::  service created?
06-13 18:17:07.696 16471-16492/com.example.ricardo.retrofittut D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-13 18:17:07.699 16471-16471/com.example.ricardo.retrofittut D/Atlas: Validating map...
06-13 18:17:07.732 16471-16492/com.example.ricardo.retrofittut I/OpenGLRenderer: Initialized EGL, version 1.4
06-13 18:17:07.732 16471-16492/com.example.ricardo.retrofittut W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
06-13 18:17:07.739 16471-16492/com.example.ricardo.retrofittut D/EGL_emulation: eglCreateContext: 0xaf034e20: maj 2 min 0 rcv 2
06-13 18:17:07.759 16471-16492/com.example.ricardo.retrofittut D/EGL_emulation: eglMakeCurrent: 0xaf034e20: ver 2 0
06-13 18:17:07.765 16471-16492/com.example.ricardo.retrofittut D/OpenGLRenderer: Enabling debug mode 0
06-13 18:17:07.775 16471-16492/com.example.ricardo.retrofittut D/EGL_emulation: eglMakeCurrent: 0xaf034e20: ver 2 0
06-13 18:17:07.818 16471-16471/com.example.ricardo.retrofittut W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
06-13 18:17:47.884 16471-16471/com.example.ricardo.retrofittut E/onFailure: WHYYYYYYY
06-13 18:17:47.941 16471-16492/com.example.ricardo.retrofittut D/EGL_emulation: eglMakeCurrent: 0xaf034e20: ver 2 0
06-13 18:17:51.393 16471-16492/com.example.ricardo.retrofittut D/EGL_emulation: eglMakeCurrent: 0xaf034e20: ver 2 0

0 个答案:

没有答案