2015-12-16 7 views
17

ライブラリcom.squareup.retrofit:retrofit:1.9.0を使用して、アンドロイドアプリケーションからサーバーにデータを送信しています。失敗:retrofit.RetrofitError:307一時的なリダイレクト?

failure : retrofit.RetrofitError: 307 Temporary Redirect 

は、私は多くのアイデアを試してみたが、同じ問題が解消されない:私は、サーバーに要求を送信する場合

は実際に、私はこのエラーを検出しました。

この問題を解決するには、私にエキスパートしてください。

よろしく

+0

改造APIを作成するためのコードを投稿してください。最新のバージョンのレトロフィットをリダイレクトをサポートしているokhttpで使ってみませんか? –

+0

@lotfi reftrofitClientのセットアップ方法、また一般的なサービスを確認できますか? – Napster

+0

Retrofit 1.9を使用する必要がある場合は、私の答えを見てください。さもなければ、私が見ているように自動的にリダイレクトを処理する2.0以上の方が簡単な方法だと思われます。 – mass

答えて

5

編集:あなたは、新しいバージョン(すなわち2.0+)に切り替え、レトロフィット1.9を使用してはならない場合は、自動的に以下の対象とソリューションを処理する必要があります。


(アンドロイド側)HTTPクライアントがあなたから打つべき対象リダイレクトURLが含まれていることになっており、この問題が発生したときに、あなたが受けている応答ヘッダーでLocation値を読み取ることで、このリダイレクトを処理する必要がありますように見えます再びクライアント。

hereを参照してください。

So for now you need to implement this at the application level (which is hard with Retrofit 1, easier with the forthcoming Retrofit 2) or wait until OkHttp 3.1(ish) when we implement the newest spec.

307も参照してください。

Fixing 307 errors - general The 307 response from the Web server should always include an alternative URL to which redirection should occur. If it does, a Web browser will immediately retry the alternative URL. So you never actually see a 307 error in a Web browser, unless perhaps you have a corrupt redirection chain e.g. URL A redirects to URL B which in turn redirects back to URL A. If your client is not a Web browser, it should behave in the same way as a Web browser i.e. immediately retry the alternative URL.

If the Web server does not return an alternative URL with the 307 response, then either the Web server sofware itself is defective or the Webmaster has not set up the URL redirection correctly.

レスポンスからLocationヘッダー値(URL)を取得するために、Retrofit 1.9.0のjavadocを参照してください。あなたは からあなたの依存関係を変更し、旧依存

を使用している

http://static.javadoc.io/com.squareup.retrofit/retrofit/1.9.0/retrofit/client/Response.html#getHeaders--

// omitting null check for brevity 
for (Header header : response.getHeaders()) 
{ 
    if (header.getName().equals("Location")) { 
     redirectURL = header.getValue(); 
    } 
} 

// do a redirect to redirectURL 
4

"com.squareup.retrofit:改造:1.9.0"

compile 'com.squareup.retrofit2:retrofit:2.2.0' 
compile 'com.squareup.retrofit2:converter-gson:2.2.0' 
compile 'com.squareup.okhttp3:okhttp:3.4.1' 

この依存関係を追加して完全ログを取得する

compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'. 

あなたのレトロフィットクライアントクラスのコードの下に

HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); 
    logging.setLevel(HttpLoggingInterceptor.Level.BODY); 
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
    httpClient.connectTimeout("Your_Time", TimeUnit.SECONDS); 
    httpClient.readTimeout("Your_Time", TimeUnit.SECONDS); 
    httpClient.addInterceptor(logging); 

    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl("BASE URL") 
      .addConverterFactory(GsonConverterFactory.create()) 
      .client(httpClient.build()) 
      .build(); 

を、このメソッドを追加します

public Response post(String url, String content) throws IOException { 
    RequestBody body = RequestBody.create(PROTOCOL, content); 

    Request.Builder requestBuilder = new Request.Builder().url(url).post(body); 
    Request request = requestBuilder.build(); 
    Response response = this.client.newCall(request).execute(); 

    if(response.code() == 307) { 
     String location = response.header("Location"); 
     return post(location, content); 
    } 

    return response; 
} 
1

HttpLoggingInterceptorを入れて、ログをチェックしてみHTTPプロトコル例外を処理することです。また、サーバーが適切な応答で戻ってきているかどうかを、パラメーターとともに提供された入力に対して返信する場合は、Rest Clientとクロスチェックしてください。HttpLoggingInterceptorについては

、これは以下

public RestClient() { 
     HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); 
     logging.setLevel(HttpLoggingInterceptor.Level.BODY); 
     OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 

     httpClient.addInterceptor(logging); 
     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl("BASEURL") 
       .addConverterFactory(GsonConverterFactory.create()) 
       .client(httpClient.build()) 
       .build(); 
     //APIInterface initiation 
     service = retrofit.create(APIInterface.class); 
    } 

を設定するには、以下の方法ですあなたはいくつかの他の可能性を、次のされている場合は、同じ

compile 'com.squareup.retrofit2:converter-gson:2.1.0' 
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1' 
compile 'com.squareup.retrofit2:retrofit:2.1.0' 

の依存関係は、レトロフィットのこのSample tutorialて行くされています残りのクライアントを開始する

0

私は多くの努力の後に解決策を得ました。これがあなたに役立つかもしれません。このエラーが発生した場合"203 HTTP 307 Temporary Redirect"このトリックが役に立ちます。 最後にWebサービスに'/'を追加し、このエラーがなくなることを確認します。私はこれの背後にある理由を知らないが、それは私のために働く。

私のために、私の以前のリクエストウェブサービス:https://mydomain/rest/Search。 このURLを使用していたのですが、"203 HTTP 307 Temporary Redirect"私はPOSTMANとウェブブラウザで回答を得ることができました。

私の新しいリクエストWebサービスのトリック:https://mydomain/rest/Search/'/'この問題を解決します。

関連する問題