【问题标题】:Get request in retrofit generates 500 internal server error改造中的获取请求会生成 500 内部服务器错误
【发布时间】:2017-02-08 20:14:32
【问题描述】:

我已将改造 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);
}

结果.java

public class Result {


    @SerializedName("category_id")
    private String entityId;

    public String getEntityId() {
        return entityId;
    }

    public void setEntityId(String entityId) {
        this.entityId = entityId;
    }

}

Postman response

【问题讨论】:

标签: android retrofit


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    像这样创建拦截器并将其添加到 okhttpCLient.addInterceptor(拦截器);

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 2015-09-20
      • 2016-09-27
      相关资源
      最近更新 更多