1

I'd like to create a Github repository that will contain only PDF files. For fetching PDF files I will use retrofit in a way described at this link:

https://futurestud.io/tutorials/retrofit-2-how-to-download-files-from-server

So right now I just wanted to list all the files from one of my Android projects on Github. (right now it only contains one PDF file)

This is the implementation:

Network module:

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

@Provides
@Singleton
Retrofit provideRetrofit(RxJavaCallAdapterFactory rxJavaCallAdapterFactory, Gson gsonConverterFactory) {
    return new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addCallAdapterFactory(rxJavaCallAdapterFactory)
            .addConverterFactory(GsonConverterFactory.create(gsonConverterFactory))
            .build();
}

Retrofit service:

@Streaming
@GET(".")
Call<List<okhttp3.ResponseBody>> getFiles();

And inside my activity, I'm trying to fetch the files:

Call<List<okhttp3.ResponseBody>> call = retrofitService.getFiles();
    call.enqueue(new Callback<List<okhttp3.ResponseBody>>() {
        @Override
        public void onResponse(@NonNull Call<List<okhttp3.ResponseBody>> call, @NonNull Response<List<okhttp3.ResponseBody>> response) {
            if (response.body() != null) {
                Toast.makeText(HomeScreenActivity.this, response.message(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(@NonNull Call<List<okhttp3.ResponseBody>> call, @NonNull Throwable t) {
            Toast.makeText(HomeScreenActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

And when I run it, I get "Expected BEGIN_ARRAY but was STRING at line 7 column 1 path $" error in onFailure() method.

After that I tried this way (without List):

@Streaming
@GET(".")
Call<okhttp3.ResponseBody> getFiles();

And inside Activity:

        Call<okhttp3.ResponseBody> call = retrofitService.getFiles();
    call.enqueue(new Callback<okhttp3.ResponseBody>() {
        @Override
        public void onResponse(@NonNull Call<okhttp3.ResponseBody> call, @NonNull Response<okhttp3.ResponseBody> response) {
            if (response.body() != null) {
                Toast.makeText(HomeScreenActivity.this, response.message(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(@NonNull Call<okhttp3.ResponseBody> call, @NonNull Throwable t) {
            Toast.makeText(HomeScreenActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

And the onResponse is called and the response is just this:

Response{
protocol=http/1.1,
code=200,
message=OK,
url=https: //github.com/username/repository/
}

Is this way of fetching PDF files even possible? If it is, can you tell me if something is not right with my implementation?

user8789149
  • 121
  • 4
  • 12
  • Did you try to add a Logging Interceptor to see what the content of the body is? See this SO answer: https://stackoverflow.com/a/33256827/1062074 Just a guess: Try `Call call = ...` instead of `Call – chrjs Dec 12 '17 at 08:34
  • "Expected BEGIN_ARRAY but was STRING at line 7 column 1 path $" this error comes when the Call is success full and you have received the data, but you are saving/handling it wrongly. It says you are expecting an Array but actually its a String . You are expecting a List , but actually its a string that is being fectched . Use postman see what response you are getting exactly. – Saurabh Kataria Dec 12 '17 at 08:36
  • @chrjs I edited my question ( used Call call = ... instead of Call – user8789149 Dec 12 '17 at 08:40
  • you should use github APIs for this. check https://developer.github.com/v3/repos/contents/ – Saurabh Dec 12 '17 at 08:41

0 Answers0