Utilizzo di retrofit
Desidero inoltrare la richiesta POST a http://milzinas.lt/oauthsilent/authorize
. Questo URL è speciale perché ti reindirizza a http://milzinas.e-bros.lt/oauthsilent/authorize
. Il mio setup di retrofit utilizza OkHttpClient
. Se faccio richiesta usando OkHttpClient solo allora il reindirizzamento funziona bene, cioè il codice di stato 401 è ricevuto. Tuttavia, quando utilizzo lo stesso OkHttpClient con retrofit, la risposta è il codice di stato 307. Penso che questo abbia a che fare con l'implementazione OkClient
che avvolge OkHttpClient ma non sono sicuro. Di seguito è riportato il codice che ho usato per testare questo scenario. Sto usando queste librerie:OkHttp non reindirizza le richieste POST quando utilizzato con retrofit
com.squareup.retrofit:retrofit:1.9.0
com.squareup.okhttp:okhttp:2.2.0
Capisco che quando URL reindirizza a un altro URL del client http deve fare due richieste. Nel mio caso la prima richiesta restituisce 307 (Redirect temporaneo) e la seconda restituisce 401 (Non autorizzato). Tuttavia, il retrofit restituisce sempre la risposta della prima richiesta. Sai come fare in modo che il reindirizzamento funzioni correttamente con il retrofit? Forse potrei ottenere questo usando un altro client HTTP? Ogni suggerimento sarà apprezzato.
Così, quando eseguo il codice qui sotto stampe console
Retrofit failure. Status: 307
OkHttp. Status: 401
voglio che sia
Retrofit failure. Status: 401
OkHttp. Status: 401
public class MainActivity extends AppCompatActivity {
interface Api {
@POST(URL)
@Headers("Accept: application/json")
void test(@Body Object dummy, Callback<Object> callback);
}
static final String BASE_URL = "http://milzinas.lt";
static final String URL = "/oauthsilent/authorize";
final OkHttpClient okHttpClient = new OkHttpClient();
Api api;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RestAdapter retrofit = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.setClient(new OkClient(okHttpClient))
.setConverter(new Converter() {
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
return null;
}
@Override
public TypedOutput toBody(Object object) {
return null;
}
})
.build();
api = retrofit.create(Api.class);
makeRequestOkHttp();
makeRequestRetrofit();
}
void makeRequestOkHttp() {
new AsyncTask<Object, Object, Object>() {
@Override
protected Object doInBackground(Object... objects) {
try {
Request request = new Request.Builder().url(BASE_URL + URL).build();
com.squareup.okhttp.Response response = okHttpClient.newCall(request).execute();
android.util.Log.d("matka", "OkHttp. Status: " + response.code());
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
}.execute();
}
void makeRequestRetrofit() {
api.test("", new Callback<Object>() {
@Override
public void success(Object o, Response response) {
android.util.Log.d("matka", "Retrofit success. Status: " + response.getStatus());
}
@Override
public void failure(RetrofitError error) {
android.util.Log.d("matka", "Retrofit failure. Status: " + error.getResponse().getStatus());
}
});
}
}
Si prega di aggiornare a un OkHttp più recente. 2.7.5 ha risolto molti problemi. –
E OkHttp 3.x è ancora meglio! –
Il problema persiste anche con le versioni più recenti di retrofit (2.0.0-beta2) e OkHttp (3.2.0). – Egis