我已将evofit 2.0与Android应用程序集成。邮递员响应适用于API,当我将其与Android应用程序集成时,它会生成内部服务器错误
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Call<List<Result>> results = RetroClient.getApiService().getResponse("application/json");
results.enqueue(new Callback<List<Result>>() {
@Override
public void onResponse(Call<List<Result>> call, Response<List<Result>> response) {
if (response.isSuccessful()) {
Toast.makeText(MainActivity.this,
String.valueOf(response.message()), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,
"ifferror", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<List<Result>> call, Throwable t) {
Toast.makeText(MainActivity.this, "failed", Toast.LENGTH_SHORT).show();
}
});
}
}
RetroClient.java
public class RetroClient {
private static final String ROOT_URL = "http://azeemkhan.me/quflip/api/rest/";
private static Retrofit restAdapter;
private static ApiService apiService;
static {
setupRestClient();
}
private static void setupRestClient() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(logging)
.build();
restAdapter = new Retrofit.Builder()
.baseUrl(ROOT_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static ApiService getApiService() {
if (apiService == null) {
apiService = restAdapter.create(ApiService.class);
}
return apiService;
}
}
ApiService.java
public interface ApiService {
@GET("products/3/categories")
Call<List<Result>> getResponse(@Header("Content-Type") String content_type);
}
Result.java
public class Result {
@SerializedName("category_id")
private String entityId;
public String getEntityId() {
return entityId;
}
public void setEntityId(String entityId) {
this.entityId = entityId;
}
}
答案 0 :(得分:0)
我相信您需要设置的Accept
标头。您还可以在构建Retrofit实例时调用以下内容:addHeader("Accept", "application/json")
答案 1 :(得分:0)
像这样创建拦截器并将其添加进去 okhttpCLient.addInterceptor(interecptor);
Interceptor interceptor = new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request newRequest;
newRequest = chain.request().newBuilder()
.addHeader("Content-Type","application/json")
.build();
return chain.proceed(newRequest);
}
};