2016-02-20 31 views
2

我使用retrofit1舊式從retrofit2

@GET("/loginUser") 
    public Call<Response> login(
      @Query("email") String email, 
      @Query("password") String password, 
      Callback<Response> callback); 

現在我不想讓「用戶」級,但是我希望得到一個String響應獲取字符串響應體。

以前我們使用的是「響應」但是,現在在retrofit2沒有反應,

我怎樣才能串響應,或從服務器,而不使用任何JSON解析全身反應?

回答

14

創建該類

import java.io.IOException; 
import java.lang.annotation.Annotation; 
import java.lang.reflect.Type; 

import okhttp3.MediaType; 
import okhttp3.RequestBody; 
import okhttp3.ResponseBody; 
import retrofit2.Converter; 
import retrofit2.Retrofit; 

public class ToStringConverterFactory extends Converter.Factory { 
    private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain"); 


    @Override 
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { 
     if (String.class.equals(type)) { 
      return new Converter<ResponseBody, String>() { 
       @Override 
       public String convert(ResponseBody value) throws IOException { 
        return value.string(); 
       } 
      }; 
     } 
     return null; 
    } 

    @Override 
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { 

     if (String.class.equals(type)) { 
      return new Converter<String, RequestBody>() { 
       @Override 
       public RequestBody convert(String value) throws IOException { 
        return RequestBody.create(MEDIA_TYPE, value); 
       } 
      }; 
     } 
     return null; 
    } 
} 

Retrofit retrofit = new Retrofit.Builder() 
         .addConverterFactory(new ToStringConverterFactory()) 
         .build(); 

編輯使用它: 你必須把它定義爲

@GET("/loginUser") 
    public Call<String> login(
      @Query("email") String email, 
      @Query("password") String password); 

沒有支持retrofit2所以回調你必須刪除它。爲了使異步的,你所要做的

Call<String> call = service.login(username, password); 
call.enqueue(new Callback<String>() {} 

編輯上面的代碼是爲retrofit2測試3.對於改裝:2.1.0,你必須創建ToStringConverterFactory爲 -

import java.io.IOException; 
import java.lang.annotation.Annotation; 
import java.lang.reflect.Type; 

import okhttp3.MediaType; 
import okhttp3.RequestBody; 
import okhttp3.ResponseBody; 
import retrofit2.Converter; 
import retrofit2.Retrofit; 

public class ToStringConverterFactory extends Converter.Factory { 
    private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain"); 


    @Override 
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { 
     if (String.class.equals(type)) { 
      return new Converter<ResponseBody, String>() { 
       @Override 
       public String convert(ResponseBody value) throws IOException { 
        return value.string(); 
       } 
      }; 
     } 
     return null; 
    } 

    @Override 
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, 
                  Annotation[] methodAnnotations, Retrofit retrofit) { 

     if (String.class.equals(type)) { 
      return new Converter<String, RequestBody>() { 
       @Override 
       public RequestBody convert(String value) throws IOException { 
        return RequestBody.create(MEDIA_TYPE, value); 
       } 
      }; 
     } 
     return null; 
    } 
} 

要知道: Incase你想要有多個轉換器(例如上面顯示的一個字符串轉換器,也是一個GSON轉換器):
確保先指定專用轉換器(例如字符串轉換器)和通用轉換器(li ke Gson)最後!

轉換器將按照它們添加的順序被調用,如果轉換器消耗了響應,則不會調用以下轉換器。

+0

和我將如何更改代碼我張貼 –

+0

也在第二功能覆蓋的網址是錯誤 –

+0

錯誤檢查您的導入。所有的進口應該是從改造2和okhttp3,而不是改造 –

1

Retrofit 2.0.0-beta3增加了一個converter-scalars模塊,它提供了一個Converter.Factory,用於將字符串,8種基元類型和8種盒裝基元類型轉換爲文本/普通物體。在普通的轉換器之前安裝它,以避免通過這些簡單的標量,例如JSON轉換器。