首页 > 解决方案 > Android Retrofit with redirection

问题描述

I am developing simple Android app where I am using google spreadsheet as a data source. For communication I am using google app script which implements doPost method because my app is sending some data to sheet and also wants some data as a response. The problem is instead of json response I always get html response about redirection in the errorBody().

I have also set OkHttpClient with redirections enabled to my retrofit service, but result is still the same.

I am working with Insomnia rest client for debugging and when I set redirections on there, everything works there fine.

If somebody had the same problem and solved it, please help.

Edit:

Here is my code:

public class Connector {

private static final String BASE_URL = "https://script.googleusercontent.com/";
private static final Object LOCK = new Object();
private static CallTaxiService service;
private static final String TAG = "Connector";

private static CallTaxiService getService()
{
    if (service == null)
    {
        synchronized(LOCK) {
            Log.d(TAG, "creating instance");
            service = buildService();
        }
    }
    return service;
}

private static CallTaxiService buildService()
{
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(new OkHttpClient.Builder().followRedirects(true)
                    .followSslRedirects(true).build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    return retrofit.create(CallTaxiService.class);
}




public static void syncData(List<TaxiServiceAppData> data, Callback<Response> callback)
{
    Call<Response> call = getService().sendData(data);
    Log.d(TAG, "syncing data");
    call.enqueue(callback);
}

private interface CallTaxiService {
    @Headers({"Content-type: application/json"})
    @POST("endpoint_url")
    Call<Response> sendData(@Body List<TaxiServiceAppData> data);
}

}

And here is how I am calling it:

            Connector.syncData(taxiServiceAppData, new retrofit2.Callback<com.adrisoft.calltaxi.model.Response>() {
            @Override
            public void onResponse(Call<com.adrisoft.calltaxi.model.Response> call, Response<com.adrisoft.calltaxi.model.Response> response) {
                com.adrisoft.calltaxi.model.Response data = response.body();
                if (data != null) {
                    newCities = data.getCities();
                    newTaxis = data.getTaxis();
                    updateDb();
                    prefs.saveSyncTime();
                    isSyncRunning = false;
                    callback.onSuccess();
                } else {
                    try {
                        Log.d(TAG, "Sync failed ... no data available. Error: " + response.errorBody().string());
                    } catch (Exception ex) {

                    }

                    callback.onFailure();
                }
            }

            @Override
            public void onFailure(Call<com.adrisoft.calltaxi.model.Response> call, Throwable t) {
                Log.d(TAG, "Sync request failed.");
                isSyncRunning = false;
                callback.onFailure();
            }
        });

And exactly in the log "Sync failed ... no data available ..." I am getting this from errorBody():

<HTML>
<HEAD>
<TITLE>Temporary Redirect</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Temporary Redirect</H1>
The document has moved <A HREF="https://script.google.com/endpoint_url">here</A>.
</BODY>
</HTML>

标签: androidgoogle-apps-scriptgoogle-sheetsretrofitokhttp3

解决方案


Redirect could have happened because the server endpoint provided https and in your code you call http. Then the server would redirect to https. Only GET requests can be redirected, so others like POST will result in error.


推荐阅读