改造不返回完整的字符串
Retrofit is not returning full String
我面对这个问题已经好几个小时了。我在这个 link 上有一个字符串 123#5432#7
,但是当我尝试通过 Retrofit
获取它时,我只得到第一个 # 之前的字符,在在这种情况下,输出是 123 但我想要整个字符串。我试着用 OkHttpClient
获取它,它工作正常。任何人都可以找到我的代码的问题或者其他可能的方法吗?
输出
123
但我想要像 123#5432#7
这样的整个字符串
编辑: 添加了 .addConverterFactory(ScalarsConverterFactory.create())
但响应相同。
代码
public class ApiClient {
public static Retrofit retrofit;
public static String BASE_URL = "https://prdec.com/status_app/";
public static Retrofit getRetrofit(){
Gson gson = new GsonBuilder()
.setLenient()
.create();
if (retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
return retrofit;
}
}
public interface ApiService {
@GET("status_app_return_string.php")
Call<String> getStringResponse();
}
ApiService apiService = ApiClient.getRetrofit().create(ApiService.class);
apiService.getStringResponse()
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
String str = response.body();
Log.d(TAG, "onResponse: "+str);
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
问题出在 .addConverterFactory(GsonConverterFactory.create(gson))
和 addConverterFactory(ScalarsConverterFactory.create())
的排名上
ScalarsConverterFactory 应该在其他转换器工厂之上。
不工作
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addConverterFactory(ScalarsConverterFactory.create())
.build();
工作
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
我面对这个问题已经好几个小时了。我在这个 link 上有一个字符串 123#5432#7
,但是当我尝试通过 Retrofit
获取它时,我只得到第一个 # 之前的字符,在在这种情况下,输出是 123 但我想要整个字符串。我试着用 OkHttpClient
获取它,它工作正常。任何人都可以找到我的代码的问题或者其他可能的方法吗?
输出
123
但我想要像 123#5432#7
编辑: 添加了 .addConverterFactory(ScalarsConverterFactory.create())
但响应相同。
代码
public class ApiClient {
public static Retrofit retrofit;
public static String BASE_URL = "https://prdec.com/status_app/";
public static Retrofit getRetrofit(){
Gson gson = new GsonBuilder()
.setLenient()
.create();
if (retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
return retrofit;
}
}
public interface ApiService {
@GET("status_app_return_string.php")
Call<String> getStringResponse();
}
ApiService apiService = ApiClient.getRetrofit().create(ApiService.class);
apiService.getStringResponse()
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
String str = response.body();
Log.d(TAG, "onResponse: "+str);
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
问题出在 .addConverterFactory(GsonConverterFactory.create(gson))
和 addConverterFactory(ScalarsConverterFactory.create())
ScalarsConverterFactory 应该在其他转换器工厂之上。
不工作
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addConverterFactory(ScalarsConverterFactory.create())
.build();
工作
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();