我正在开发一个Android项目。我正在使用改造来调用API。所以我正在进行API调用以获取与证书相关的课程列表。
来自服务器get(BASE_URL / certificates / 1)的响应是:
[ValidateInput(false)]
public JsonResult GetMessageDetails(string bodyContent)
{
ViewBag.bodyContent = bodyContent;
return Json(bodyContent, JsonRequestBehavior.AllowGet);
}
我有证书列表,所需的操作是,在点击证书时,它应该采用新的片段,其中包含与此证书相关的所有课程(点击该片段)。为此,我进行了上述API调用。
问题是当我点击列表中的第一个证书时,它会转到下一个片段并列出所有课程。但是,当我单击任何其他证书时,它会显示空白片段。当我调试代码时,在fetchCourses() CoursesListOfCertificatesFragment.java 中,调用onFailure()函数。
工作流程是CertificateFragment,它是列出所有证书的类。然后单击,将触发CertificateFragment中的CertificateClickListner(),然后启动CoursesListOfCertificatesFragment,并在此片段的构造函数中传递证书对象。在这里我进行API调用GET(BASE_URL / certificates / 1)。
这是在CoursesListOfCertificatesFragment中的fetchCourses()中。但是除了第一个证书,调用onFailure()方法。
感谢任何帮助.....如果需要任何其他信息,请询问。
感谢。
以下是列出的相关课程:
CoursesListOfCertificatesFragment.java
{
"id": 1,
"name": "Arts-certificate",
"authorized_by": "priya",
"created_by": "self",
"created_by_id": "1",
"description": "awarded to the best",
"doc_url": "http://f8rentals.com/wp-content/uploads/2015/03/IMG_0067ft.jpg?189db0",
"category_id": "2",
"cateogory": "Arts",
"courses": [
{
"id": 1,
"course_id": "CRSXQHGZ0SCTLhklhk6JL",
"number_of_sessions": 1,
"created_at": "2015-12-16T07:10:37.000+00:00",
"updated_at": "2015-12-16T07:10:37.000+00:00",
"tags": null
},
{
"id": 1,
"course_id": "CRSXQHGZ0SCTLhklhk6JL",
"number_of_sessions": 1,
"created_at": "2015-12-16T07:10:37.000+00:00",
"updated_at": "2015-12-16T07:10:37.000+00:00",
"tags": null
}
.
.
.
.
]
}
CertificateFragment.java
package com.localjini.learner.fragments;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.localjini.learner.R;
import com.localjini.learner.adapters.CourseListingRVAdapter;
import com.localjini.learner.api.RestClient;
import com.localjini.learner.main.HomeActivity;
import com.localjini.learner.models.Certificates;
import com.localjini.learner.models.Course;
import com.localjini.learner.models.CoursesResponse;
import com.localjini.learner.models.GetCertificateResponse;
import com.localjini.learner.utils.LogUtils;
import com.localjini.learner.utils.UIHelper;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit.Call;
import retrofit.Callback;
import retrofit.Response;
import retrofit.Retrofit;
public class CoursesListOfCertificatesFragment extends BaseFragment {
@Bind(R.id.tv_category)
TextView tvCategory;
@Bind(R.id.tv_sort_filter)
TextView tvSortFilter;
@Bind(R.id.pb_course_listing)
ProgressBar pbCourseListing;
@Bind(R.id.rv_course_listing)
RecyclerView rvCourseListing;
public UIHelper uiHelper;
List<Course> courses;
private Certificates certificate;
private int certificate_id;
public CoursesListOfCertificatesFragment(){
}
private CourseListingRVAdapter.CourseClickListener listener = new CourseListingRVAdapter.CourseClickListener() {
@Override
public void onCourseClicked(Course course) {
interactionListener.launchCourseDetailFragment();
}
};
public CoursesListOfCertificatesFragment(Certificates certificate){
this.certificate = certificate;
this.certificate_id = certificate.getId();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (root == null) {
root = (ViewGroup) inflater.inflate(R.layout.fragment_course_listing, container, false);
ButterKnife.bind(this, root);
initInstances();
fetchCourses();
}
return root;
}
private void initInstances() {
uiHelper = new UIHelper(getActivity());
((HomeActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((HomeActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((HomeActivity) getActivity()).getSupportActionBar().setTitle("");
rvCourseListing.setItemAnimator(new DefaultItemAnimator());
rvCourseListing.setLayoutManager(new LinearLayoutManager(getActivity()));
rvCourseListing.setHasFixedSize(true);
}
private void fetchCourses() {
Call<GetCertificateResponse> call = RestClient.getCMSServiceInstance().getCertificateById(certificate_id);
call.enqueue(new Callback<GetCertificateResponse>() {
@Override
public void onResponse(Response<GetCertificateResponse> response, Retrofit retrofit) {
if (response != null) {
GetCertificateResponse resp = response.body();
if (resp != null) {
showCourses(resp.getCourses());
} else {
uiHelper.showToast("response contain null data");
}
}
}
@Override
public void onFailure(Throwable t) {
LogUtils.d("coursesFailed", t.getMessage());
}
});
}
private void showCourses(List<Course> courses) {
if(courses != null) {
CourseListingRVAdapter courseListingRVAdapter = new CourseListingRVAdapter(getActivity(), courses);
courseListingRVAdapter.setCourseClickListener(listener);
rvCourseListing.setAdapter(courseListingRVAdapter);
}
}
public FragmentId getFragmentId() {
return FragmentId.COURSE_LISTING_FRAGMENT;
}
}
答案 0 :(得分:0)
我解决了这个问题。原来服务器给出了错误的响应(它仍处于测试阶段)。我期待着一系列的字符串。对于第一个证书,所有课程都有字符串数组,但是对于第二个版本,所有课程都有字符串值而不是数组。