改造未从 api 获取数据
Retrofit not getting data from api
我有一些 api,其中包含 JSON 和合作伙伴名称。
我已经设置了接口和 pojo 模型。我会 post 在我的代码下方。
这是我的 POJO 模型合作伙伴:
public class Partner {
@SerializedName("name")
@Expose
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
现在这是我的界面:
public interface APIService {
@GET("Partner")
Call<List<Partner>> getPartners();
}
这是我的 APIHelper:
public class APIHelper {
public static final String PARTNERS_URL = "https://part-of-url.herokuapp.com/partners.json/";
public static APIService apiService;
public static APIService getApiService() {
if (apiService == null) {
Retrofit retrofit = new Retrofit.Builder().baseUrl(PARTNERS_URL)
.addConverterFactory(GsonConverterFactory.create()).build();
apiService = retrofit.create(APIService.class);
}
return apiService;
}
}
这是包含 Button 的 Fragment,其中 onClick 方法需要从 Web 获取数据。
public class DownloadMain extends Fragment implements Callback<Partner> {
private static final String TAG = DownloadMain.class.getSimpleName();
private Button dloadPartners;
private Call callPartners;
public DownloadMain() {}
public DownloadMain newInstance() { return new DownloadMain(); }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.download_main, container, false);
dloadPartners = (Button) view.findViewById(R.id.downloadPartners);
dloadPartners.setOnClickListener(btnListener);
callPartners = APIHelper.getApiService().getPartners();
return view;
}
Button.OnClickListener btnListener = (new Button.OnClickListener() {
@Override
public void onClick(View v) {
callPartners.clone().enqueue(DownloadMain.this);
}
});
@Override
public void onResponse(Call call, Response response) {
if(response.body() == null) {
try {
response.errorBody().string();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getActivity(), "No Partners!", Toast.LENGTH_SHORT).show();
} else {
List<String> partners = (List<String>) response.body();
Log.d(TAG, "Number of partners received: " + partners.size());
Toast.makeText(getActivity(), "Partners downloaded!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call call, Throwable t) {
}
}
这是我从网络获取合作伙伴的所有代码。
我在服务器端收到此错误且无法解决:
No route matches [GET] "/partners.json/Partner"
问题:谁能看看这段代码并说出哪里出了问题,为什么我没有从 Web 获取数据,还有我应该如何解决这个无路由错误?
像这样更改您的获取路径
public interface APIService {
@GET("partners.json")
Call<List<Partner>> getPartners();
}
并从您的基础中删除该路径 url。
public class APIHelper {
public static final String PARTNERS_URL = "https://part-of-url.herokuapp.com/";
public static APIService apiService;
public static APIService getApiService() {
if (apiService == null) {
Retrofit retrofit = new Retrofit.Builder().baseUrl(PARTNERS_URL)
.addConverterFactory(GsonConverterFactory.create()).build();
apiService = retrofit.create(APIService.class);
}
return apiService;
}
}
我有一些 api,其中包含 JSON 和合作伙伴名称。
我已经设置了接口和 pojo 模型。我会 post 在我的代码下方。
这是我的 POJO 模型合作伙伴:
public class Partner {
@SerializedName("name")
@Expose
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
现在这是我的界面:
public interface APIService {
@GET("Partner")
Call<List<Partner>> getPartners();
}
这是我的 APIHelper:
public class APIHelper {
public static final String PARTNERS_URL = "https://part-of-url.herokuapp.com/partners.json/";
public static APIService apiService;
public static APIService getApiService() {
if (apiService == null) {
Retrofit retrofit = new Retrofit.Builder().baseUrl(PARTNERS_URL)
.addConverterFactory(GsonConverterFactory.create()).build();
apiService = retrofit.create(APIService.class);
}
return apiService;
}
}
这是包含 Button 的 Fragment,其中 onClick 方法需要从 Web 获取数据。
public class DownloadMain extends Fragment implements Callback<Partner> {
private static final String TAG = DownloadMain.class.getSimpleName();
private Button dloadPartners;
private Call callPartners;
public DownloadMain() {}
public DownloadMain newInstance() { return new DownloadMain(); }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.download_main, container, false);
dloadPartners = (Button) view.findViewById(R.id.downloadPartners);
dloadPartners.setOnClickListener(btnListener);
callPartners = APIHelper.getApiService().getPartners();
return view;
}
Button.OnClickListener btnListener = (new Button.OnClickListener() {
@Override
public void onClick(View v) {
callPartners.clone().enqueue(DownloadMain.this);
}
});
@Override
public void onResponse(Call call, Response response) {
if(response.body() == null) {
try {
response.errorBody().string();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getActivity(), "No Partners!", Toast.LENGTH_SHORT).show();
} else {
List<String> partners = (List<String>) response.body();
Log.d(TAG, "Number of partners received: " + partners.size());
Toast.makeText(getActivity(), "Partners downloaded!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call call, Throwable t) {
}
}
这是我从网络获取合作伙伴的所有代码。
我在服务器端收到此错误且无法解决:
No route matches [GET] "/partners.json/Partner"
问题:谁能看看这段代码并说出哪里出了问题,为什么我没有从 Web 获取数据,还有我应该如何解决这个无路由错误?
像这样更改您的获取路径
public interface APIService {
@GET("partners.json")
Call<List<Partner>> getPartners();
}
并从您的基础中删除该路径 url。
public class APIHelper {
public static final String PARTNERS_URL = "https://part-of-url.herokuapp.com/";
public static APIService apiService;
public static APIService getApiService() {
if (apiService == null) {
Retrofit retrofit = new Retrofit.Builder().baseUrl(PARTNERS_URL)
.addConverterFactory(GsonConverterFactory.create()).build();
apiService = retrofit.create(APIService.class);
}
return apiService;
}
}