2015-12-16 20 views
17

Android uygulamamdan sunucuma veri göndermek için com.squareup.retrofit:retrofit:1.9.0 kütüphanesini kullanıyorum.başarısızlık: retrofit.RetrofitError: 307 Geçici Yönlendirme?

failure : retrofit.RetrofitError: 307 Temporary Redirect 

Birçok fikir denemek ama aynı sorun devam: Ben sunucuya bir istek göndermek istediğinizde

Aslında, bu hatayı buldum.

Lütfen bu sorunu çözmek için bana yardımcı olun.

Selamlar

+0

Güçlendirme api oluşturma kodunu kaydedin. Sadece bilgi için neden yeniden yönlendirme için daha fazla desteğe sahip okhttp ile retrofit'in son sürümünü kullanmıyorsunuz? –

+0

@lotfi reftrofitClient kurulum yönteminizi, ayrıca ortak hizmeti görebilir miyim. – Napster

+0

Retrofit 1.9'u kullanmanız gerekiyorsa, cevabımıma bakın. Aksi halde gördüğüm gibi otomatik olarak 307 üzerinde yönlendirme işleyen 2.0+ ile gitmek için daha kolay bir yol gibi görünüyor. – mass

cevap

5

Düzenleme: Eğer yeni bir sürüme geçiş Retrofitler 1.9, kullanmamalısınız (yani 2.0+) otomatik olarak aşağıda ele çözüm işlemek gerekir.


aralarından vurmak gerektiğini bu hedef yönlendirme URL'si içeren gerekiyordu, hangi olduğunda aldığınız cevap başlığında Location değerini okuyarak bu yönlendirmeyi işlemesi gereken (Android tarafında) HTTP istemcisi gibi görünüyor müşteri tekrar.

Onların görüşüne bakın 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.

da neyi 307 araçları bakın.

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.

bakınız javadoc karşılık gelen konum başlık değerine (URL) kapmak için Recommended 1.9.0 için; Sen adresinin bağımlılık değiştirme Eski bağımlılık

kullanma are

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: güçlendirme: 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' 

için

da, tüm günlüğü almak için bu bağımlılığı ekleyin

compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'. 
Kodun altında

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

Http protokolü istisna HttpLoggingInterceptor koymak ve günlükleri kontrol etmek

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

deneyin Kulp etmektir sizin Güçlendirmesi İşveren Class bu yöntemi ekleyin. Ayrıca sunucunuz, parametrelerle birlikte sağlanan giriş için uygun yanıtla geri dönmüyorsa Rest Client ile çapraz kontrol edin.HttpLoggingInterceptor için

, bu bazı diğer olasılıkları takip ediyorlarsa Aşağıda

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

, Retrofit bu Sample tutorial üzerinden aynı

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' 

Go için bağımlılıkları vardır yapılandırmak için aşağıdaki yoludur Geri kalan istemcinin başlatılması

0

Çok fazla çabadan sonra çözümü aldım. Belki bu size yardımcı olur. Bu hata "203 HTTP 307 Geçici Yönlendirme" alıyorsanız, bu numara size yardımcı olacaktır. Sonunda web hizmetinize '/' ekleyin ve sonra bu hatanın giderilip giderilmediğini kontrol edin. Bunun arkasındaki sebebi bilmiyorum ama benim için çalışıyor.

Benim için eski istek web hizmetim: https://mydomain/rest/Search. Bu URL'yi kullanarak POSTMAN ve web tarayıcısında bir yanıt alabildiğim "203 HTTP 307 Geçici Yönlendirme" oluyordum.

Yeni istek web sitemle hile: https://mydomain/rest/Search/. Bu '/' sorunu çözme.

İlgili konular