0

I got that error message: End of input at line 1 column 1 path $

I am new in android.

There is something that I can get in postman as json but I can't get in android via retrofit2.

This is the function, that causes the problem

@GET("getRequestsByPassenger/{id}")
    suspend fun getRequestsByPassenger(
        @Path("id") id: Int
    ) : List<ServiceRequest>

This is the Retrofit2:

private val okHttpClient = OkHttpClient.Builder()
        .addInterceptor { chain ->
            var request = chain.request()

            if (!token.isNullOrEmpty()) {
                request = request.newBuilder()
                    .addHeader("Authorization", AUTH)
                    .build()
            }

            chain.proceed(request)
        }.build()

    val gson = GsonBuilder().setLenient().create()

    val INSTANCE: Api by lazy {
        val retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .client(okHttpClient)
            .build()

        retrofit.create(Api::class.java)
    }

I am looking that problem the whole day. Thank you for your help!

1 Answers1

0

TL;DR This is more than likely an issue with gson formatting your network request, make sure it is correctly formatted

Gson is expecting your JSON string to begin with an object opening brace. e.g.

{

But the string you have passed to it starts with an open quotes

"

Explanation

This sounds like an issue with GSON parsing your network request

This link shows how this exact issue can be created and has a post to how how to resolve Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

Now, if you add this to your retrofit, it gives you another error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

This is another well-known error you can find answer here (this error means that your server response is not well-formatted);

So change server response to return something:

{ android:[ { ver:"1.5", name:"Cupcace", api:"Api Level 3" } ... ] }

This answer leads you to another answer "Expected BEGIN_OBJECT but was STRING at line 1 column 1"

Which is the issue that i am expected to see

Gson is expecting your JSON string to begin with an object opening brace. e.g.

{ But the string you have passed to it starts with an open quotes

"

Brandon
  • 538
  • 6
  • 16