改造2.0,将GET请求到.json文件作为终结点 [英] Retrofit 2.0, request GET to a .json file as endpoint
问题描述
您好,我正在使用Retrofit 2.0进行测试,其中一项测试是请求以.json结尾的网址:
Hello Im working in a test with Retrofit 2.0 and one of the test is making a resquest to a url that finish with .json:
示例: https://domain.com/contacts.json
baseURl: https://domain.com/ 终点:/contacts.json
baseURl: https://domain.com/ endPoint: /contacts.json
这是一个文件,但我想发出一个普通的GET请求并直接将json放入其中
Which is a file, but I want to make a normal GET request and get the json inside directly
推荐答案
您好,我找到了一种使用您的代码来获取文件的解决方案,它现在确实可以使用,但是我还没有接触Web服务器上的MIME,我想我我猜没有在代码中添加Converter.谢谢.
Hello I found a solution to get the file using your code and it really works now but I haven't touch the MIME on the web server, I think I didn't have added the Converter in the code I guess. Thank you.
WebAPIService.java:
WebAPIService.java:
public interface WebAPIService {
@GET("/contacts.json")
Call<JsonObject> getContacts();
}
MainAcitivty.java:
MainAcitivty.java:
Retrofit retrofit1 = new Retrofit.Builder()
.baseUrl(BuildConfig.API_ENDPOINT)
.addConverterFactory(GsonConverterFactory.create())
.build();
WebAPIService service1 = retrofit1.create(WebAPIService.class);
Call<List<Contact>> jsonCall = service1.getContacts();
jsonCall.enqueue(new Callback<List<Contact>() {
@Override
public void onResponse(Call<List<Contact>> call, Response<List<Contact>> response) {
Log.i(LOG_TAG, response.body().toString());
}
@Override
public void onFailure(Call<List<Contact>> call, Throwable t) {
Log.e(LOG_TAG, t.toString());
}
});
这篇关于改造2.0,将GET请求到.json文件作为终结点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!