改造没有从api

时间:2016-08-25 14:12:46

标签: android retrofit2

我有一些包含带有合作伙伴名称的JSON的api。

我已经设置了界面和pojo模型。我将在我的代码下面发布。

这是我的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) {    
    }
}

这是我从网络上获取合作伙伴的所有代码。

我在服务器端收到此错误但无法解决此问题:

  

没有路线匹配[GET]&#34; /partners.json/Partner"

问题:有人可以查看此代码并说明错误以及为什么我没有从网络获取数据以及我该如何解决此无路由错误?

1 个答案:

答案 0 :(得分:1)

改变你的获取路径

public interface APIService {    
    @GET("partners.json")
    Call<List<Partner>> getPartners();
}

并从基本网址中删除该路径。

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