首页 文章

在改造中获取请求会产生 500 内部服务器错误

提问于
浏览
0

我已经整合了改装 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;
    }

}

邮递员的回应

2 回答

  • 0

    我相信这是你需要设置的Accept标题。您还可以在构建 Retrofit 实例时调用以下内容:addHeader("Accept", "application/json")

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

相关问题