【问题标题】:Android Retrofit with redirection带有重定向的 Android 改造
【发布时间】:2018-09-23 21:18:56
【问题描述】:

我正在开发简单的 Android 应用程序,我使用谷歌电子表格作为数据源。对于通信,我正在使用实现 doPost 方法的谷歌应用程序脚本,因为我的应用程序正在向工作表发送一些数据并且还需要一些数据作为响应。问题是我总是在 errorBody() 中得到关于重定向的 html 响应,而不是 json 响应。

我还为我的改造服务设置了启用重定向的 OkHttpClient,但结果仍然相同。

我正在使用 Insomnia 休息客户端进行调试,当我在那里设置重定向时,一切正常。

如果有人遇到同样的问题并解决了,请帮忙。

编辑:

这是我的代码:

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);
}

}

我是这样称呼它的:

            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();
            }
        });

而正是在日志“同步失败......没有可用的数据......”我从 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>

【问题讨论】:

  • 您能否添加一个无法遵循重定向的代码的最小示例?这与google-apps-scriptgoogle-spreadsheet 有什么关系?由于问题已经提出,根本没有任何关系。此外,它可能与以下内容重复:stackoverflow.com/questions/18218726/…
  • 发布您的 doPost 代码。 baseUrl 不应该是script.google.com 而不是usercontent.com
  • 我试过了。还是一样的结果:/

标签: android google-apps-script google-sheets retrofit okhttp3


【解决方案1】:

重定向可能会发生,因为服务器端点提供了https,并且在您的代码中您调用了http。然后服务器将重定向到https。只能重定向GET请求,所以POST等其他请求会报错。

【讨论】:

  • 您需要发布您的代码。你有没有:OkHttpClient client = new OkHttpClient().newBuilder() .followRedirects(true) .followSslRedirects(true) .build();
猜你喜欢
  • 2012-02-07
  • 1970-01-01
  • 2017-12-08
  • 2019-01-03
  • 2017-11-01
  • 1970-01-01
  • 2018-02-09
  • 2016-07-12
  • 1970-01-01
相关资源
最近更新 更多