【发布时间】: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;
}
}
【问题讨论】: